-
Notifications
You must be signed in to change notification settings - Fork 0
/
LINKED-LIST_IMPLEMENTATION.cpp
333 lines (299 loc) · 4.81 KB
/
LINKED-LIST_IMPLEMENTATION.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
struct node* link;
};
struct node* root=NULL;
int len;
void append(void);
void addatbegin(void);
void addatpos(void);
int length(void);
void display(void);
void deletion(void);
void search(void);
void reverse(void);
void Sort(struct node *) ;
int main()
{
int ch;
while(1)
{
cout<<"---------------------------\n";
cout<<"1. Append\n";
cout<<"2. Insert at begin\n";
cout<<"3. Insert at postion\n";
cout<<"4. Length\n";
cout<<"5. Display\n";
cout<<"6. Delete\n";
cout<<"7. Search\n";
cout<<"8. Reverse\n";
cout<<"9. Sort\n";
cout<<"10. Quit\n";
cout<<"Enter your choice : \n";
cin>>ch;
switch(ch)
{
case 1: append();
break;
case 2: addatbegin();
break;
case 3: addatpos();
break;
case 4: len=length();
cout<<"Length is : \n"<<len<<endl;
break;
case 5: display();
break;
case 6: deletion();
break;
case 7: search();
break;
case 8: reverse();
break;
case 9: Sort(root);
break;
case 10: exit(1);
default :
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid input\n";
}
}
return 0;
}
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node *p;
p=root;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}
}
void addatbegin()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
temp->link=root;
root=temp;
}
}
void addatpos()
{
struct node *temp,*p;
int loc,i=1;
cout<<"Enter location : \n";
cin>>loc;
if(loc>length()+1)
{
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid location... \n";
cout<<"Currently list has "<<length()<<" nodes\n";
}
else
{
p=root;
while(i<loc-1)
{
p=p->link;
i++;
}
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->link=NULL;
temp->link=p->link;
p->link=temp;
}
}
int length()
{
int c=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
c++;
temp=temp->link;
}
return c;
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
system("cls");
cout<<"---------------------------\n";
cout<<"List is empty... \n";
}
else
{
system("cls");
cout<<"Elements are : \n";
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->link;
}
cout<<"\n";
}
}
void deletion()
{
if(root==NULL)
{
cout<<"No elements to be deleted...\n";
}
else
{
struct node* temp;
int loc;
cout<<"Enter the location to delete : \n";
cin>>loc;
if(loc>length())
cout<<"Invalid location...\n";
else if(loc==1)
{
temp=root;
root=temp->link;
temp->link=NULL;
free(temp);
}
else
{
struct node*p=root,*q;
int i=1;
while(i<loc-1)
{
p=p->link;
i++;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);
}
}
}
void search()
{
int ele,k=1;
if(root==NULL)
{
cout<<"No elements to be searched...\n";
}
else
{
int count=0;
cout<<"Enter element to be searched : \n";
cin>>ele;
struct node* temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==ele)
{
cout<<"Element found at position "<<k<<endl;
count++;
break;
}
k++;
temp=temp->link;
}
if(count==0)
{
cout<<"Element is not present...\n";
}
}
}
void reverse()
{
if(root==NULL)
{
system("cls");
cout<<"---------------------------\n";
cout<<"List is empty... \n";
}
else
{
system("cls");
int i=0,j,len,k,tempvar;
struct node *p,*q;
len=length();
j=len-1;
p=q=root;
while(i<j)
{
k=0;
while(k<j)
{
q=q->link;
k++;
}
tempvar=p->data;
p->data=q->data;
q->data=tempvar;
i++;
j--;
p=p->link;
q=root;
}
display();
}
}
void Sort(struct node *start)
{
struct node *temp1,*temp2;
int k;
/* Checking for empty list */
if (start == NULL)
cout<<"Nothing to sort...\n";
else
{
for(int i=length()-2;i>=0;i--)
{
temp1=start;
temp2=temp1->link;
for(int j=0;j<=i;j++)
{
if(temp1->data > temp2->data)
{
k=temp1->data;
temp1->data=temp2->data;
temp2->data=k;
}
temp1=temp2;
temp2=temp2->link;
}
}
}
}