-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_linkedlist.cpp
More file actions
312 lines (310 loc) · 5.08 KB
/
new_linkedlist.cpp
File metadata and controls
312 lines (310 loc) · 5.08 KB
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
#include<stdio.h>
#include<stdlib.h>
typedef struct linklist
{
int n;
struct linklist *next;
}node;
node *head=NULL;
void create()
{
int x;
char ch;
node *t,*h;
do
{
printf("Enter the value: ");
scanf("%d",&x);
t=(node*)malloc(sizeof(node));
t->n=x;
t->next=NULL;
if(head==NULL)
{
head=t;
h=t;
}
else
{
h->next=t;
h=t;
}
printf("Do you want to Continue??? ");
fflush(stdin);
x=getchar();
}
while(x=='y'||x=='Y');
}
void display()
{
node *d=head;
if(head==NULL)
printf("List is empty. Nothing to display.");
else
{
printf("The values are: ");
while(d!=NULL)
{
printf("%d ",d->n);
d=d->next;
}
}
}
node*search(int x)
{
node*s=head;
while(s!=NULL)
{
if(s->n==x)
return s;
else
s=s->next;
}
return NULL;
}
int count()
{
int c=0;
node *a=head;
while(a!=NULL)
{
c++;
a=a->next;
}
return c;
}
void deletebyvalue(int x)
{
node *p,*h;
p=search(x);
if(p==NULL)
printf("Value not Found!!!");
else
{
if(head->n==x||head==p)
head=head->next;
else
{
h=head;
while(h->next!=p)
h=h->next;
h->next=p->next;
}
free(p);
}
}
void deletebyposition(int pos)
{
int c=count();
node *p=head,*h=head;
if(pos<=0||pos>c)
printf("Invalid Position");
else
{
if(pos==1)
head=head->next;
else
{
for(int i=1;i<=pos-2;i++)
h=h->next;
p=p->next;
h->next=p->next;
}
free(p);
}
}
void insertatposition(int o,int pos)
{
int n=count();
node *t,*p;
if(pos<1||pos==n+1)
printf("Invalid Position!!!!");
else
{
t=(node*)malloc(sizeof(node));
t->n=o;
if(pos==1)
{
t->next=head;
head=t;
}
else
{
p=head;
for(int i=1;i<=pos-2;i++)
p=p->next;
t->next=p->next;
p->next=t;
}
}
}
void insertbefore(int r,int e)
{
node *p,*h,*t;
p=search(e);
if(p==NULL)
printf("NOT FOUND!!!!");
else
{
t=(node*)malloc(sizeof(node));
t->n=r;
if(head==p)
{
t->next=p;
head=t;
}
else
{
h=head;
while(h->next!=p)
{
h=h->next;
}
h->next=t;
t->next=p;
}
}
}
void insertafter(int q,int w)
{
node *p=search(w),*t;
if(p==NULL)
printf("Given value NOT FOUND!!!!!");
else
{
t=(node*)malloc(sizeof(node));
t->n=q;
t->next=p->next;
p->next=t;
}
}
void reverse()
{
node *t1,*t2,*t3;
if(head==NULL||head->next==NULL)
{
return;
}
t1=head;
t2=t1->next;
t3=t2->next;
t1->next=NULL;
while(t2!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
if(t3!=NULL)
t3=t3->next;
}
head=t1;
}
void menu()
{
printf("\n");
printf("Program on Single LinkList:\n");
printf("1.Create.\n");
printf("2.Display.\n");
printf("3.Search.\n");
printf("4.Count.\n");
printf("5.Delete By Value.\n");
printf("6.Delete By Position.\n");
printf("7.Insert at Position.\n");
printf("8.Insert Before.\n");
printf("9.Insert After.\n");
printf("10.Reverse.\n");
printf("11.EXIT\n");
printf("Enter your choice: ");
}
int main()
{
int ch,x,n,z,o,pos,e,pos2,w,pos3;//n for count x for display z for delete by position pos and o for insert at position e and r for inster before w and pos3 for insert after
node *b;
while(87)
{
menu();
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
system("pause");
system("cls");
break;
case 2:
display();
system("pause");
system("cls");
break;
case 3:
if(head==NULL)
printf("List is Empty. Nothing to display.");
else
{
printf("Enter th value: ");
scanf("%d",&x);
b=search(x);
if(b==NULL)
printf("Searched element NOT FOUND!!!!");
else
printf("FOUND.");
}
system("pause");
system("cls");
break;
case 4:
n=count();
printf("Number of node is %d \n",n);
system("pause");
system("cls");
break;
case 5:
printf("Enter the value you want to delete: ");
scanf("%d",&x);
deletebyvalue(x);
system("pause");
system("cls");
break;
case 6:
printf("Enter the Position you want to delete: ");
scanf("%d",&z);
deletebyposition(z);
system("pause");
system("cls");
break;
case 7:
printf("Enter the value: ");
scanf("%d",&o);
printf("Enter the value to insert at postion: ");
scanf("%d",&pos);
insertatposition(o,pos);
system("pause");
system("cls");
break;
case 8:
printf("Enter the value: ");
scanf("%d",&e);
printf("Enter the value to insert before: ");
scanf("%d",&pos2);
insertbefore(e,pos2);
system("pause");
system("cls");
break;
case 9:
printf("Enter the value: ");
scanf("%d",&w);
printf("Enter the value to insert after: ");
scanf("%d",&pos3);
insertafter(w,pos3);
system("pause");
system("cls");
break;
case 10:
reverse();
system("pause");
system("cls");
break;
case 11:
exit(0);
}
}
}