-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_Operations.c
More file actions
389 lines (371 loc) · 7.96 KB
/
Linked_List_Operations.c
File metadata and controls
389 lines (371 loc) · 7.96 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
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
386
387
388
389
#include <stdio.h>
#include <stdlib.h>
typedef struct node //structure for node
{
int data; //data field
struct node *next; //next field(link)
}nodes;
nodes *getnode() //function to create one node
{
int i;
nodes *temp;
temp=(nodes*)malloc(sizeof(nodes)); //Allocating memory to node
printf("Enter the data: ");
scanf("%d",&i);
temp -> data=i;
temp -> next=NULL;
return temp;
}
void create(nodes *head) //Function to link all created nodes
{
int i;
char ch='n';
nodes *temp,*last=NULL;
temp=(nodes*)malloc(sizeof(nodes)); //Allocating memory to node
printf("Do you want to create new node?(y/n): ");
scanf(" %c",&ch);
while(ch=='y')
{
temp=getnode();
if(last==NULL)
{
last=temp;
head -> next=last; //Linking head node with first node
}
else
{
last->next=temp; //Linking all other created nodes
last=temp;
}
printf("Do you want to create new node?(y/n): ");
scanf(" %c",&ch);
}
}
void display(nodes *head) //Function to display all data in list
{
nodes *curr;
curr=head->next;
printf("The contents of linked list are:\n");
if(head -> next==NULL) //if head->next is NULL list is empty
printf("The list is empty!!\n");
else if(curr==NULL)
{
printf("%d\t",curr -> data); //else print data by links
curr=curr -> next;
}
else
{
while(curr!=NULL)
{
printf("%d\t",curr -> data);
curr=curr -> next;
}
}
printf("\n");
}
int length(nodes *head) //Function to find length of list
{
nodes *curr;
curr=head->next;
int cnt=0;
if(head->next==NULL)
return 0;
else
{
while(curr!=NULL) //incrementing count till current->next is NULL
{
cnt++;
curr=curr->next;
}
return cnt;
}
}
void insert(nodes *head) //Function to insert node at any position in list
{
nodes *curr=head,*nnode;
int i=1,k=length(curr),pos;
printf("Enter the position to which a node to be inserted: ");
scanf("%d",&pos);
nnode=getnode();
if(pos>k+1) //position is greater than length+1, node can't be inserted
printf("Data can't be inserted!!\n");
else
{
while(curr!=NULL&&i<pos) //else take current pointer to position
{
curr=curr->next;
i++;
}
nnode->next=curr->next; //insert node at that position
curr->next=nnode; //Adjusting links
printf("Node inserted successfully!!\n");
}
display(head);
}
void delete(nodes *head) //Function to delete node from any position
{
nodes *prev=head;
int i=0,k=length(prev),pos;
printf("Enter the position from which a node to be deleted: ");
scanf("%d",&pos);
if(pos>k) //If position is greater than length, node can't be deleted
printf("Node can't be deleted!!\n");
else
{
while(prev!=NULL&&i<pos-1)
{
prev=prev->next; //else take previous pointer to position
i++;
}
nodes *temp=prev->next;
prev->next=temp->next; //adjusting links
free(temp); //freeing memory allocated for node
printf("The node deleted successfully!!\n");
}
display(head);
}
void reverse(nodes *head) //Function to reverse the linked list
{
nodes *ptr=NULL,*mid=NULL,*prev=NULL;
ptr=head->next;
while(ptr!=NULL)
{
prev=mid;
mid=ptr;
ptr=ptr->next; //reversing the list by adjusting links
mid->next=prev;
}
head->next=mid; //Linking head with first element of reversed list
printf("The list is reversed successfully!!\n");
display(head);
}
void sort(nodes *head) //Function to sort the data
{
nodes *p,*q;
int data;
q=head->next;
while(q!=NULL)
{
p=q->next;
while(p!=NULL)
{
if(q->data > p->data) //Using selection sort to swap data items and links.
{
data=p->data;
p->data=q->data;
q->data=data;
}
p=p->next;
}
q=q->next;
}
printf("The list is sorted successfully!!\n");
display(head);
}
void merge(nodes *head1,nodes *head2,nodes *head3) //Function to Merge 2 lists
{
nodes *curr1=head1->next,*curr2=head2->next,*last=NULL;
while(curr1!=NULL&&curr2!=NULL)
{
if(last==NULL)
{
if(curr1->data > curr2->data) //Comparing data
{
last=curr2;
head3->next=last; //Attaching last node to head
curr2=curr2->next;
}
else
{
last=curr1;
head3->next=last;
curr1=curr1->next;
}
}
else
{
if(curr1->data > curr2->data) //sorting and merging
{
last->next=curr2;
last=curr2;
curr2=curr2->next;
}
else
{
last->next=curr1;
last=curr1;
curr1=curr1->next;
}
}
}
if(curr1!=NULL) //Attaching remaining list
{
last->next=curr1;
}
if(curr2!=NULL)
{
last->next=curr2;
}
printf("Lists merged sucessfully!!\n");
display(head3);
}
int main()
{
int i,choice;
nodes *head=(nodes*)malloc(sizeof(nodes)); //creating 3 head nodes and linking them to null
head -> next=NULL;
nodes *head1=(nodes*)malloc(sizeof(nodes));
head1 -> next=NULL;
nodes *head2=(nodes*)malloc(sizeof(nodes));
head2 -> next=NULL;
printf("Create the list.\n\n");
create(head);
while(1)
{
printf("\n****** MENU ******\n");
printf("1.Display list.\n2.Length of list.\n3.Insert node.\n4.Delete node.\n5.Reverse the list.\n6.Sort the list.\n7.Merge 2 lists.\n8.Exit.\n\nEnter the choice: "); //Different choices for different operations
scanf("%d",&choice);
switch(choice)
{
case 1: display(head);
break;
case 2: i=length(head);
printf("Length of linked list is: %d\n",i);
break;
case 3: insert(head);
break;
case 4: delete(head);
break;
case 5: reverse(head);
break;
case 6: sort(head);
break;
case 7: printf("Create the second list.\n\n");
create(head1);
merge(head,head1,head2);
break;
case 8: exit(1);
break;
default:printf("Wrong Choice!!\nPlease enter correct choice.\n");
}
}
return 0;
}
/*
OUTPUT:
Create the list.
Do you want to create new node?(y/n): y
Enter the data: 10
Do you want to create new node?(y/n): y
Enter the data: 20
Do you want to create new node?(y/n): y
Enter the data: 30
Do you want to create new node?(y/n): n
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 1
The contents of linked list are:
10 20 30
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 2
Length of linked list is: 3
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 3
Enter the position to which a node to be inserted: 2
Enter the data: 15
Node inserted successfully!!
The contents of linked list are:
10 15 20 30
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 4
Enter the position from which a node to be deleted: 2
The node deleted successfully!!
The contents of linked list are:
10 20 30
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 5
The list is reversed successfully!!
The contents of linked list are:
30 20 10
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 6
The list is sorted successfully!!
The contents of linked list are:
10 20 30
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 7
Create the second list.
Do you want to create new node?(y/n): y
Enter the data: 21
Do you want to create new node?(y/n): y
Enter the data: 34
Do you want to create new node?(y/n): n
Lists merged sucessfully!!
The contents of linked list are:
10 20 21 30 34
****** MENU ******
1.Display list.
2.Length of list.
3.Insert node.
4.Delete node.
5.Reverse the list.
6.Sort the list.
7.Merge 2 lists.
8.Exit.
Enter the choice: 8
*/