This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked List.cpp
152 lines (148 loc) · 2.66 KB
/
Linked List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
using namespace std;
struct node{
int key;
struct node* next;
struct node* back;
};
template <class T> class LinkList{
private:
struct node* last;
struct node* first;
T num;
public:
LinkList(){
num=0;
first=new struct node;
first -> next=NULL;
first -> back=NULL;
last=first;
}
int &operator [](int op){
struct node* temp=first;
for(int i=0;i<op;i++){
temp=temp -> next;
}
return temp -> key;
}
struct node* at(int n){
if(n>=num)
return NULL;
struct node* temp=first;
for(int i=0;i<n;i++){
temp=temp -> next;
}
return temp;
}
int find(T k){
struct node* temp=first;
int index=0;
while(temp -> key!=k){
temp=temp -> next;
index++;
if(index==num)
return -1;
}
return index;
}
void pushBack(T n){
struct node* temp=new struct node;
if(num==0){
first=temp;
}
temp -> key=n;
temp -> next=NULL;
temp -> back=last;
last -> next=temp;
last=temp;
num++;
}
void pushTop(T n){
if(num==0){
this -> pushBack(n);
return;
}
struct node* temp=new struct node;
temp -> key=n;
temp -> next=first;
temp -> back=NULL;
first -> back=temp;
first=temp;
num++;
}
int size(){
return num;
}
void remove(int n){
if(n!=0&&n!=num-1){
struct node* temp=this -> at(n-1);
((temp -> next) -> next) -> back=temp;
temp -> next=(temp -> next) -> next;
}
else if(n==0){
first=first -> next;
first -> back=NULL;
}
else{
(last -> back) -> next=NULL;
last=last -> back;
}
num--;
}
void insert(T k,int n){
if(num==0){
this -> pushBack(k);
return;
}
if(n==num-1){
this -> pushBack(k);
return;
}
if(n==-1){
this -> pushTop(k);
return;
}
struct node* temp1=this -> at(n);
struct node* temp2=new struct node;
temp2 -> next=temp1->next;
temp2 -> key=k;
temp1 -> next=temp2;
num++;
}
};
int main(int argc,char* argv[]){
LinkList <int> A;
int n;
if(argv[1]==NULL){
cout<<"please enter switch -h -e -r -f"<<endl;
return -1;
}
if(*argv[1]=='-'&&*(argv[1]+1)=='h'){
cout<<"this is test program for my linked list"<<endl;
cout<<"-e enter n and then enter n key"<<endl;
cout<<"-f enter key and get index"<<endl;
cout<<"-r enter index and key then in linked list reaplace key in index with new key"<<endl;
return 0;
}
if(*argv[1]=='-'&&*(argv[1]+1)=='e'){
cin>>n;
for(int i=0; i<n; i++){
int x;
cin>>x;
A.pushBack(x);
}
}
/*int m;
cin>>m;
A.remove(m-1);
int k;
cin>>k;
A.insert(k,m-2);
*/for(int i=0;i<n;i++){
cout<<A[i]<<" ";
}/*
int f;
cin>>f;
cout<<A.find(f)+1<<endl;
cout<<endl;*/
}