-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsingle-linked-list.c
358 lines (317 loc) · 6.63 KB
/
single-linked-list.c
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *createList(struct node *start);
void displayList(struct node *start);
void countNodes(struct node *start);
void search(struct node *start,int x);
struct node *insertInBeginning(struct node *start,int data);
void insertAtEnd(struct node *start,int data);
void insertAfter(struct node *start,int data,int x);
struct node *insertBefore(struct node *start,int data,int x );
struct node *insertAtPosition(struct node *start,int data,int k);
struct node *deleteNode(struct node *start,int data);
struct node *reverseList(struct node *start);
main()
{
struct node *start=NULL;
int choice,data,x,k;
start = createList(start);
while(1)
{
printf("\n");
printf("1.Display list\n");
printf("2.Count the number of nodes\n");
printf("3.Search for an element\n");
printf("4.Insert in empty list / Insert in beginning of the list\n");
printf("5.Insert a node at the end of the list\n");
printf("6.Insert a node after a specified node\n");
printf("7.Insert a node before a specified node\n");
printf("8.Insert a node at a given position\n");
printf("9.Delete a node\n");
printf("10.Reverse the list\n");
printf("11.Quit\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
if(choice==11)
break;
switch(choice)
{
case 1:
displayList(start);
break;
case 2:
countNodes(start);
break;
case 3:
printf("Enter the element to be searched : ");
scanf("%d",&data);
search(start,data);
break;
case 4:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=insertInBeginning(start,data);
break;
case 5:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
insertAtEnd(start,data);
break;
case 6:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
printf("Enter the element after which to insert : ");
scanf("%d",&x);
insertAfter(start,data,x);
break;
case 7:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
printf("Enter the element before which to insert : ");
scanf("%d",&x);
start=insertBefore(start,data,x);
break;
case 8:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
printf("Enter the position at which to insert : ");
scanf("%d",&k);
start=insertAtPosition(start,data,k);
break;
case 9:
printf("Enter the element to be deleted : ");
scanf("%d",&data);
start=deleteNode(start, data);
break;
case 10:
start=reverseList(start);
break;
default:
printf("Wrong choice\n");
}
}
}/*End of main()*/
void insertAfter(struct node *start,int data,int x)
{
struct node *temp,*p;
p=start;
while(p!=NULL)
{
if(p->info==x)
break;
p=p->link;
}
if(p==NULL)
printf("%d not present in the list\n",x);
else
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=p->link;
p->link=temp;
}
}
struct node *insertBefore(struct node *start,int data,int x)
{
struct node *temp,*p;
/*If list is empty*/
if(start==NULL)
{
printf("List is empty\n");
return start;
}
/*x is in first node,new node is to be inserted before first node*/
if(x==start->info)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
return start;
}
/*Find pointer to predecessor of node containing x*/
p=start;
while(p->link!=NULL)
{
if(p->link->info==x)
break;
p=p->link;
}
if(p->link==NULL)
printf("%d not present in the list\n",x);
else
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=p->link;
p->link=temp;
}
return start;
}
struct node *insertAtPosition(struct node *start,int data,int k)
{
struct node *temp,*p;
int i;
if(k==1)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
return start;
}
p=start;
for(i=1; i<k-1 && p!=NULL; i++) /*find a pointer to k-1 node*/
p=p->link;
if(p==NULL)
printf("You can insert only upto %dth position\n\n",i);
else
{
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=p->link;
p->link=temp;
}
return start;
}
struct node *createList(struct node *start)
{
int i,n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
if(n==0)
return start;
printf("Enter the first element to be inserted : ");
scanf("%d",&data);
start=insertInBeginning(start,data);
for(i=2; i<=n; i++)
{
printf("Enter the next element to be inserted : ");
scanf("%d",&data);
insertAtEnd(start,data);
}
return start;
}/*End of createList()*/
struct node *insertInBeginning(struct node *start,int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
return start;
}
void insertAtEnd(struct node *start,int data)
{
struct node *p,*temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=temp;
temp->link=NULL;
}
void displayList(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
printf("List is : ");
p=start;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n");
}/*End of displayList()*/
void countNodes(struct node *start)
{
int n=0;
struct node *p=start;
while(p!=NULL)
{
n++;
p=p->link;
}
printf("Number of nodes in list = %d\n",n);
}/*End of countNodes() */
void search(struct node *start,int x)
{
struct node *p;
int position=1;
p=start;
while(p!=NULL)
{
if(p->info==x)
break;
position++;
p=p->link;
}
if(p==NULL)
printf("%d not found in list\n",x);
else
printf("%d is at position %d\n",x,position);
}/*End of search()*/
struct node *deleteNode(struct node *start,int x)
{
struct node *temp,*p;
if(start==NULL)
{
printf("List is empty\n");
return start;
}
/*Deletion of first node*/
if(start->info==x)
{
temp=start;
start=start->link;
free(temp);
return start;
}
/*Deletion in between or at the end*/
p=start;
while(p->link!=NULL)
{
if(p->link->info==x)
break;
p=p->link;
}
if(p->link==NULL)
printf("Element %d not in list\n\n",x);
else
{
temp=p->link;
p->link=temp->link;
free(temp);
}
return start;
}
struct node *reverseList(struct node *start)
{
struct node *prev, *ptr, *next;
prev=NULL;
ptr=start;
while(ptr!=NULL)
{
next=ptr->link;
ptr->link=prev;
prev=ptr;
ptr=next;
}
start=prev;
return start;
}/*End of reverseList()*/