-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly linkedlist.c
330 lines (280 loc) · 7.3 KB
/
singly linkedlist.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node* create(int n);
void print(node *p);
void count(node *p);
void search(node *p);
node* insert_at_Begin(node *head);
node* insert_at_Middle(node *head);
node* insert_at_End(node *head);
node* delete_at_Begin(node *head);
node* delete_at_Middle(node *head);
node* delete_at_End(node *head);
int main() {
node *Head = NULL;
int no, op;
do {
printf("\n************SLL OPERATIONS*************");
printf("\n1.Create\n2.Print\n3.Count\n4.Search");
printf("\n5.Insert at Begin\n6.Insert at Middle\n7.Insert at End");
printf("\n8.Delete from Begin\n9.Delete from Middle\n10.Delete from End\n11.Exit");
printf("\nEnter your choice: ");
scanf("%d", &op);
switch(op) {
case 1:
printf("\nEnter size of linked list: ");
scanf("%d", &no);
Head = create(no);
break;
case 2:
print(Head);
break;
case 3:
count(Head);
break;
case 4:
search(Head);
break;
case 5:
Head = insert_at_Begin(Head);
break;
case 6:
Head = insert_at_Middle(Head);
break;
case 7:
Head = insert_at_End(Head);
break;
case 8:
Head = delete_at_Begin(Head);
break;
case 9:
Head = delete_at_Middle(Head);
break;
case 10:
Head = delete_at_End(Head);
break;
case 11:
printf("\nGood Bye..Thanks for using our Application!!!\n");
break;
default:
printf("\nPlease select a valid option!!!\n");
}
} while(op != 11);
return 0;
}
node* create(int n) {
node *head = NULL, *p = NULL, *temp = NULL;
int i, x;
if (n <= 0) {
printf("\nInvalid size!\n");
return NULL;
}
printf("\nEnter data: ");
scanf("%d", &x);
head = (node*)malloc(sizeof(node));
if (head == NULL) {
printf("\nMemory allocation failed!\n");
return NULL;
}
head->data = x;
head->next = NULL;
p = head;
for (i = 2; i <= n; i++) {
printf("\nEnter data: ");
scanf("%d", &x);
temp = (node*)malloc(sizeof(node));
if (temp == NULL) {
printf("\nMemory allocation failed!\n");
return head; // Return the partially created list
}
temp->data = x;
temp->next = NULL;
p->next = temp;
p = temp;
}
printf("\nLinked list created successfully:\n");
return head;
}
void print(node *p) {
if (p == NULL) {
printf("\nThe list is empty.\n");
return;
}
printf("\nSLL Nodes:\n");
while(p != NULL) {
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
void count(node *p) {
int count = 0;
while(p != NULL) {
count++;
p = p->next;
}
printf("\nNumber of Nodes: %d\n", count);
}
void search(node *p) {
int key, flag = 0;
printf("\nEnter key element to be searched: ");
scanf("%d", &key);
while(p != NULL) {
if (key == p->data) {
flag = 1;
break;
}
p = p->next;
}
if (flag)
printf("\nElement is found!!!\n");
else
printf("\nElement is not found!!!\n");
}
node* insert_at_Begin(node *head) {
node *q;
int x;
printf("\nEnter data: ");
scanf("%d", &x);
q = (node*)malloc(sizeof(node));
if (q == NULL) {
printf("\nMemory allocation failed!\n");
return head;
}
q->data = x;
q->next = head;
head = q;
printf("\nNode inserted at Begin successfully!!!\n");
return head;
}
node* insert_at_End(node *head) {
node *q, *p;
int x;
printf("\nEnter data: ");
scanf("%d", &x);
q = (node*)malloc(sizeof(node));
if (q == NULL) {
printf("\nMemory allocation failed!\n");
return head;
}
q->data = x;
q->next = NULL;
if (head == NULL) {
head = q;
} else {
p = head;
while(p->next != NULL) {
p = p->next;
}
p->next = q;
}
printf("\nNode inserted at end successfully!!!\n");
return head;
}
node* insert_at_Middle(node *head) {
node *q, *p;
int x, loc, i;
if (head == NULL) {
printf("\nLinked list is empty!!! You cannot insert a node in the middle!!!\n");
return NULL;
}
printf("\nEnter data: ");
scanf("%d", &x);
q = (node*)malloc(sizeof(node));
if (q == NULL) {
printf("\nMemory allocation failed!\n");
return head;
}
q->data = x;
q->next = NULL;
printf("\nEnter location for insertion: ");
scanf("%d", &loc);
if (loc <= 0) {
printf("\nInvalid location!\n");
free(q);
return head;
}
p = head;
for (i = 1; i < loc - 1; i++) {
if (p == NULL) {
printf("\nLocation out of bounds!\n");
free(q);
return head;
}
p = p->next;
}
q->next = p->next;
p->next = q;
printf("\nNode inserted at middle successfully!!!\n");
return head;
}
node* delete_at_Begin(node *head) {
node *q;
if (head == NULL) {
printf("\nLinked list is empty!!! You cannot delete a node!!!\n");
return NULL;
}
q = head;
head = head->next;
free(q);
printf("\nNode deleted from Begin successfully!!!\n");
return head;
}
node* delete_at_End(node *head) {
node *q, *p;
if (head == NULL) {
printf("\nLinked list is empty!!! You cannot delete a node!!!\n");
return NULL;
}
if (head->next == NULL) {
free(head);
return NULL;
}
p = head;
while(p->next->next != NULL) {
p = p->next;
}
q = p->next;
p->next = NULL;
free(q);
printf("\nNode deleted from End successfully!!!\n");
return head;
}
node* delete_at_Middle(node *head) {
node *q, *p;
int loc, i;
if (head == NULL) {
printf("\nLinked list is empty!!! You cannot delete a node!!!\n");
return NULL;
}
printf("\nEnter location of node for deletion: ");
scanf("%d", &loc);
if (loc <= 0) {
printf("\nInvalid location!\n");
return head;
}
if (loc == 1) {
return delete_at_Begin(head);
}
p = head;
for (i = 1; i < loc - 1; i++) {
if (p == NULL || p->next == NULL) {
printf("\nLocation out of bounds!\n");
return head;
}
p = p->next;
}
q = p->next;
if (q == NULL) {
printf("\nLocation out of bounds!\n");
return head;
}
p->next = q->next;
free(q);
printf("\n node delete succesfully !!!");
return(head);
}