-
Notifications
You must be signed in to change notification settings - Fork 11
/
Circular_LL.c
385 lines (358 loc) · 8.72 KB
/
Circular_LL.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
typedef struct node n;
n* create_node(int);
void add_node();
void insert_at_first();
void insert_at_end();
void insert_at_position();
void delete_node_position();
void sort_list();
void update();
void search();
void display_from_beg();
void display_in_rev();
n *new, *ptr, *prev;
n *first = NULL, *last = NULL;
int number = 0;
void main()
{
int ch;
printf("\n linked list\n");
printf("1.insert at beginning \n 2.insert at end\n 3.insert at position\n4.sort linked list\n 5.delete node at position\n 6.updatenodevalue\n7.search element \n8.displaylist from beginning\n9.display list from end\n10.exit ");
while (1)
{
printf("\n enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1 :
insert_at_first();
break;
case 2 :
insert_at_end();
break;
case 3 :
insert_at_position();
break;
case 4 :
sort_list();
break;
case 5 :
delete_node_position();
break;
case 6 :
update();
break;
case 7 :
search();
break;
case 8 :
display_from_beg();
break;
case 9 :
display_in_rev();
break;
case 10 :
exit(0);
case 11 :
add_node();
break;
default:
printf("\ninvalid choice");
}
}
}
/*
*MEMORY ALLOCATED FOR NODE DYNAMICALLY
*/
n* create_node(int info)
{
number++;
new = (n *)malloc(sizeof(n));
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
}
/*
*ADDS NEW NODE
*/
void add_node()
{
int info;
printf("\nenter the value you would like to add:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
last->next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
}
}
/*
*INSERTS ELEMENT AT FIRST
*/
void insert_at_first()
{
int info;
printf("\nenter the value to be inserted at first:");
scanf("%d",&info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially it is empty linked list later insertion is done");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
new->next = first;
first->prev = new;
first = new;
first->prev = last;
last->next = first;
printf("\n the value is inserted at begining");
}
}
/*
*INSERTS ELEMNET AT END
*/
void insert_at_end()
{
int info;
printf("\nenter the value that has to be inserted at last:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially the list is empty and now new node is inserted but at first");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
last->next = new;
new->prev = last;
last = new;
first->prev = last;
last->next = first;
}
}
/*
*INSERTS THE ELEMENT AT GIVEN POSITION
*/
void insert_at_position()
{
int info, pos, len = 0, i;
n *prevnode;
printf("\n enter the value that you would like to insert:");
scanf("%d", &info);
printf("\n enter the position where you have to enter:");
scanf("%d", &pos);
new = create_node(info);
if (first == last && first == NULL)
{
if (pos == 1)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
printf("\n empty linked list you cant insert at that particular position");
}
else
{
if (number < pos)
printf("\n node cant be inserted as position is exceeding the linkedlist length");
else
{
for (ptr = first, i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (i == pos-1)
{
prevnode->next = new;
new->prev = prevnode;
new->next = ptr;
ptr->prev = new;
printf("\ninserted at position %d succesfully", pos);
break;
}
}
}
}
}
/*
*SORTING IS DONE OF ONLY NUMBERS NOT LINKS
*/
void sort_list()
{
n *temp;
int tempval, i, j;
if (first == last && first == NULL)
printf("\nlinked list is empty no elements to sort");
else
{
for (ptr = first,i = 0;i < number;ptr = ptr->next,i++)
{
for (temp = ptr->next,j=i;j<number;j++)
{
if (ptr->val > temp->val)
{
tempval = ptr->val;
ptr->val = temp->val;
temp->val = tempval;
}
}
}
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
printf("\n%d", ptr->val);
}
}
/*
*DELETION IS DONE
*/
void delete_node_position()
{
int pos, count = 0, i;
n *temp, *prevnode;
printf("\n enter the position which u wanted to delete:");
scanf("%d", &pos);
if (first == last && first == NULL)
printf("\n empty linked list you cant delete");
else
{
if (number < pos)
printf("\n node cant be deleted at position as it is exceeding the linkedlist length");
else
{
for (ptr = first,i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (pos == 1)
{
number--;
last->next = prevnode->next;
ptr->prev = prevnode->prev;
first = ptr;
printf("%d is deleted", prevnode->val);
free(prevnode);
break;
}
else if (i == pos - 1)
{
number--;
prevnode->next = ptr->next;
ptr->next->prev = prevnode;
printf("%d is deleted", ptr->val);
free(ptr);
break;
}
}
}
}
}
/*
*UPDATION IS DONE FRO GIVEN OLD VAL
*/
void update()
{
int oldval, newval, i, f = 0;
printf("\n enter the value old value:");
scanf("%d", &oldval);
printf("\n enter the value new value:");
scanf("%d", &newval);
if (first == last && first == NULL)
printf("\n list is empty no elemnts for updation");
else
{
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
{
if (ptr->val == oldval)
{
ptr->val = newval;
printf("value is updated to %d", ptr->val);
f = 1;
}
}
if (f == 0)
printf("\n no such old value to be get updated");
}
}
/*
*SEARCHING USING SINGLE KEY
*/
void search()
{
int count = 0, key, i, f = 0;
printf("\nenter the value to be searched:");
scanf("%d", &key);
if (first == last && first == NULL)
printf("\nlist is empty no elemnets in list to search");
else
{
for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
{
count++;
if (ptr->val == key)
{
printf("\n the value is found at position at %d", count);
f = 1;
}
}
if (f == 0)
printf("\n the value is not found in linkedlist");
}
}
/*
*DISPLAYING IN BEGINNING
*/
void display_from_beg()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty no elemnts to print");
else
{
printf("\n%d number of nodes are there", number);
for (ptr = first, i = 0;i < number;i++,ptr = ptr->next)
printf("\n %d", ptr->val);
}
}
/*
* DISPLAYING IN REVERSE
*/
void display_in_rev()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty there are no elments");
else
{
for (ptr = last, i = 0;i < number;i++,ptr = ptr->prev)
{
printf("\n%d", ptr->val);
}
}
}