-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircular_linked_list.c
331 lines (328 loc) · 20.9 KB
/
circular_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
#include<stdio.h>
#include<stdlib.h>
struct node* create(struct node* start);
struct node* display(struct node* start);
struct node* insertatbeg(struct node* start);
struct node* insertatend(struct node* start);
struct node* insertbeforepos(struct node* start);
struct node* insertafterpos(struct node* start);
struct node* deleteatbeg(struct node* start);
struct node* deleteatend(struct node* start);
struct node* deletebeforepos(struct node* start);
struct node* deleteafterpos(struct node* start);
struct node* reverse(struct node* start);
struct node* size(struct node* start);
struct node* search(struct node* start);
struct node{ int data; struct node *next; };
struct node *start=NULL; void main()
{
int option;
printf("1.Create a circular linked list \n");
printf("2.Display a circular linked list \n");
printf("3.Insert at the begining of a circular linked list \n");
printf("4.Insert at the end of a circular linked list \n");
printf("5.Insert before a particular position \n");
printf("6.Insert after a particular position \n");
printf("7.Delete a node at the begining of a circular linked list \n");
printf("8.Delete a node at the end of a circular linked list \n");
printf("9.Delete before a particular position \n");
printf("10.Delete after a particular position \n");
printf("11.Calculate the number of elements of the circular linked list \n");
printf("12.Search an element in the circular linked list \n");
printf("13.Reverse a circular linked list \n");
while(1)
{
printf("Choose an option from the following: \n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("Enter values and to finish entering, press -1 on terminal!\n");
start = create(start);
break;
case 2:
display(start);
break;
case 3:
start = insertatbeg(start);
display(start);
break;
case 4:
start = insertatend(start);
display(start);
break;
case 5:
start = insertbeforepos(start);
display(start);
break;
case 6:
start = insertafterpos(start);
display(start);
break;
case 7:
start = deleteatbeg(start);
display(start);
break;
case 8:
start = deleteatend(start);
display(start);
break;
case 9:
start = deletebeforepos(start);
display(start);
break;
case 10:
start = deleteafterpos(start);
display(start);
break;
case 13:
start = reverse(start);
display(start);
break;
case 11:
start=size(start);
break;
case 12:
start=search(start);
break;
default:
printf("Enter correct option!!\n");
}
}
}
struct node* create(struct node* start)
{
int num;
struct node *newnode,*ptr;
// printf("Enter data:\n");
scanf("%d",&num);
while(num!=-1)
{
newnode=(struct node*)calloc(1,sizeof(struct node));
newnode->data=num;
if(start==NULL)
{
start=newnode;
newnode->next=start;
}
else
{ ptr=start;
while(ptr->next!=start)
{
ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=start;
}
scanf("%d",&num);
}
return start;
}
struct node* display(struct node* start)
{
struct node *ptr;
ptr=start;
if (start == NULL)
{
//printf("Circular linked list is empty.\n");
return start;
}
do
{ printf("%d --> ", ptr->data);
ptr = ptr->next;
}
while (ptr != start);
printf("START");
printf("\n");
return start;
}
struct node* insertatbeg(struct node* start)
{
struct node *newnode, *ptr;
int num;
printf("Enter the value you want to insert at the beginning: ");
scanf("%d", &num);
newnode = (struct node*)calloc(1, sizeof(struct node));
newnode->data = num;
if (start == NULL)
{
newnode->next = newnode;
// Circular link to itself for the first node
} else
{
ptr = start;
while (ptr->next != start)
{
ptr = ptr->next;
}
ptr->next = newnode;
newnode->next = start;
}
start = newnode;
// Update the start pointer return start;
}
struct node* insertatend(struct node* start)
{ struct node *newnode,*ptr;
int num;
printf("Enter the value you want to insert at end: ");
scanf("%d",&num);
ptr=start;
newnode=(struct node*)calloc(1,sizeof(struct node));
newnode->data=num;
while(ptr->next!=start)
{ ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=start;
return start;
}
struct node* insertbeforepos(struct node* start)
{ struct node *newnode,*ptr,*preptr;
int num;
int val;
printf("Ente the value before which you want to insert: \n");
scanf("%d",&val);
printf("Enter the value to be inserted: \n");
scanf("%d",&num);
ptr=start;
newnode=(struct node*)calloc(1,sizeof(struct node));
newnode->data=num;
while(ptr->data!=val)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=newnode;
newnode->next=ptr;
return start; }
struct node*
insertafterpos(struct node* start)
{
struct node *newnode,*ptr,*preptr;
int num;
int val;
printf("Enter the value after which you want to insert: \n");
scanf("%d",&val);
printf("Enter the value to be inserted: \n");
scanf("%d",&num);
ptr=start;
newnode=(struct node*)calloc(1,sizeof(struct node));
newnode->data=num;
while(ptr->data!=val)
{ ptr=ptr->next;
} preptr=ptr->next;
ptr->next=newnode;
newnode->next=preptr;
return start;
} struct node* deleteatbeg(struct node* start)
{
if (start == NULL)
{ //printf("Circular linked list is empty. Cannot delete.\n");
return start;
}
struct node *ptr = start;
if (start->next == start)
{
free(start);
start = NULL;
}
else {
while (ptr->next != start)
{ ptr = ptr->next;
} ptr->next = start->next;
struct node *temp = start;
start = start->next;
free(temp);
} return start;
} struct node* deleteatend(struct node* start)
{ struct node *ptr,*preptr;
ptr=start;
while(ptr->next!=start)
{
preptr=ptr;
ptr=ptr->next;
}
preptr->next=start;
free(ptr);
return start;
}
struct node* deletebeforepos(struct node* start)
{
struct node *ptr,*preptr,*p1;
ptr=start;
preptr=start;
int val;
printf("Enter the value before which you want to delete the node: \n");
scanf("%d",&val);
while(ptr->data!=val)
{ p1=preptr;
preptr=ptr;
ptr=ptr->next;
}
p1->next=ptr;
free(preptr);
return start;
} struct node* deleteafterpos(struct node* start)
{ struct node *ptr,*preptr;
ptr=start;
preptr=start;
int val;
printf("Enter the value after which you want to delete the node: \n");
scanf("%d",&val);
while(ptr->data!=val)
{ ptr=ptr->next;
} preptr=ptr->next;
ptr->next=preptr->next;
free(preptr);
return start;
} struct node* size(struct node* start)
{
struct node *ptr;
ptr=start;
int i=1;
while(ptr->next!=start)
{ ptr=ptr->next;
i++;
}
printf("The linked list has %d elements. \n",i);
return start;
}
struct node* reverse(struct node* start)
{
if (start == NULL || start->next == start)
{
return start;
} struct node *prevnode = NULL;
struct node *currentnode = start;
struct node *nextnode = NULL;
do
{ nextnode = currentnode->next;
currentnode->next = prevnode;
prevnode = currentnode;
currentnode = nextnode;
}
while (currentnode != start);
start = prevnode;
return start;
}
struct node* search(struct node* start)
{
struct node *ptr;
ptr=start;
int i=1;
int val;
printf("Enter the value you want to search in the linked list: \n");
scanf("%d",&val)
while(ptr->next!=start)
{ if(ptr->data==val)
{
printf("The element %d is present at %d position in the linked list.\n",val,i);
}
i++;
ptr=ptr->next;
}
if(ptr->data==val)
{
printf("The element %d is present at %d position in the linked list.\n",val,i);
}
}