-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp5_SinglyLinkedList
262 lines (255 loc) · 6.49 KB
/
Exp5_SinglyLinkedList
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
// SRUSHTI B. PATIL (IT - A - 43)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
// Defining Structure
typedef struct node
{
int data;
struct node *next;
} node;
node *createList();
node *Insert_beg(node *head, int x);
node *Insert_end(node *head, int x);
node *Insert_mid(node *head, int x);
node *Delete_beg(node *head);
node *Delete_end(node *head);
node *Delete_mid(node *head);
void PrintList(node *head);
// Main Function
void main()
{
int choice, insert_option, delete_option, x;
node *head = NULL;
printf("Welcome to the implementation of the singly linked list ! \n");
do
{
printf("Please select an operation to perform from the below list \n");
printf(" 1. Create a List \n 2. Insert a node \n 3. Delete a node \n 4. Print the existing list \n 5. Exit \n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("\n \n");
switch (choice)
{
case 1:
head = createList();
break;
case 2:
do
{
printf("Select a position where you to want to insert new node \n");
printf(" 1. Beginning of the List \n 2. At the end of the list \n 3. Insert in between \n
4. Exit the insert operation \n");
printf("Enter your choice: ");
scanf("%d", &insert_option);
switch (insert_option)
{
case 1:
printf("Enter the data to be inserted: ");
scanf("%d", &x);
head = Insert_beg(head, x);
break;
case 2:
printf("Enter the data to be inserted: ");
scanf("%d", &x);
head = Insert_end(head, x);
break;
case 3:
printf("Enter the data to be inserted: ");
scanf("%d", &x);
head = Insert_mid(head, x);
break;
case 4:
printf("Insert operation Exit");
break;
default:
printf("Please enter a valid choide: 1, 2, 3, 4");
}
} while (insert_option != 4);
printf("\n \n");
break;
case 3:
do
{
printf("Select a position from where you to want to delete the element \n");
printf(" 1. Beginning of the List \n 2. At the end of the list \n 3. Somewhere in between \n
4. Exit the delete operation \n");
printf("Enter your choice: ");
scanf("%d", &delete_option);
switch (delete_option)
{
case 1:
head = Delete_beg(head);
break;
case 2:
head = Delete_end(head);
break;
case 3:
head = Delete_mid(head);
break;
case 4:
printf("Delete Operation Exit");
break;
default:
printf("Please enter a valid choide: 1, 2, 3, 4");
}
} while (delete_option != 4);
printf("\n \n");
break;
case 4:
PrintList(head);
break;
case 5:
printf("Exit: Program Finished !!");
break;
default:
printf("Please enter a valid choide: 1, 2, 3, 4, 5");
}
} while (choice != 5);
}
// Function to create List
node *createList()
{
node *head, *p;
int i, n;
head = NULL;
printf("Enter the number of nodes: ");
scanf("%d", &n);
printf("Enter the data: ");
for (i = 0; i <= n - 1; i++)
{
if (head == NULL)
{
p = head = (node *)malloc(sizeof(node));
}
else
{
p->next = (node *)malloc(sizeof(node));
p = p->next;
}
p->next = NULL;
scanf("%d", &(p->data));
}
printf("\n \n");
return (head);
}
// Function to insert element
node *Insert_beg(node *head, int x)
{
node *p;
p = (node *)malloc(sizeof(node));
p->data = x;
p->next = head;
head = p;
return (head);
}
node *Insert_end(node *head, int x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->data = x;
p->next = NULL;
if (head == NULL)
return (p);
for (q = head; q->next != NULL; q = q->next)
;
q->next = p;
return (head);
}
node *Insert_mid(node *head, int x)
{
node *p, *q;
int y;
p = (node *)malloc(sizeof(node));
p->data = x;
p->next = NULL;
printf("After which element you want to insert the new element ?");
scanf("%d", &y);
for (q = head; q != NULL && q->data != y; q = q->next)
;
if (q != NULL)
{
p->next = q->next;
q->next = p;
}
else
printf("ERROR !! Data Not Found");
return (head);
}
// Function to delete element
node *Delete_beg(node *head)
{
node *p, *q;
if (head == NULL)
{
printf("Empty Linked List");
return (head);
}
p = head;
head = head->next;
free(p);
return (head);
}
node *Delete_end(node *head)
{
node *p, *q;
if (head == NULL)
{
printf("Empty Linked List");
return (head);
}
p = head;
if (head->next == NULL)
{
head = NULL;
free(p);
return (head);
}
for (q = head; q->next->next != NULL; q = q->next)
p = q->next;
q->next = NULL;
free(p);
return (head);
}
node *Delete_mid(node *head)
{
node *p, *q;
int x, i;
if (head == NULL)
{
printf("Empty Linked List");
return (head);
}
printf("Enter the data to be deleted: ");
scanf("%d", &x);
if (head->data == x)
{
p = head;
head = head->next;
free(p);
return (head);
}
for (q = head; q->next->data != x && q->next != NULL; q = q->next)
if (q->next == NULL)
{
printf("ERROR !! Data Not Found");
return (head);
}
p = q->next;
q->next = q->next->next;
free(p);
return (head);
}
// Function to print the existing list
void PrintList(node *head)
{
node *p;
printf("[ ");
for (p = head; p != NULL; p = p->next)
{
printf("%d \t", p->data);
}
printf(" ]");
printf("\n \n");
}