-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList1.c
288 lines (235 loc) · 5.7 KB
/
LinkedList1.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
/*Single Linked List*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void insertNodeFirst(int value)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("The %d value Is Inserted\n", value);
}
void insertNodeLast(int value)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
if (newNode == NULL)
{
printf("Overflow\n");
return;
}
else
{
newNode->data = value;
if (head == NULL)
{
newNode->next = NULL;
head = newNode;
printf("The Value %d is Inserted\n", value);
}
else
{
head = temp;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
newNode->next = NULL;
printf("The Value %d Is Inserted\n", value);
}
}
}
void RandomInsert(int value)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
int position;
if (newNode == NULL)
{
printf("Overflow\n");
return;
}
else
{
printf("Enter The Position To Inserted ? ");
scanf("%d", &position);
newNode->data = value;
temp = head;
for (int i = 0; i < position; ++i)
{
temp = temp->next;
if (temp == NULL)
{
printf("Cannot Insert At %d this Location\n", position);
}
}
newNode->next = temp->next;
temp->next = newNode;
printf("The %d is Insert at %d Position successfully\n");
}
}
void DeleteFront()
{
struct Node *ptr;
if (ptr == NULL)
{
printf("Overflow\n");
return;
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("The Node Deleted From The Begining\n");
}
}
void DeleteLast()
{
struct Node *ptr, *ptr1;
if (head == NULL)
{
printf("List Is Empty\n");
return;
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("Only One Node In The List Is Deleted\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr->next;
}
ptr1->next = NULL;
free(ptr);
printf("Deleted Node From The Last\n");
}
}
void DeleteRandom(int key)
{
struct Node *current = head, *prev = NULL;
while (current != NULL && current->data != key)
{
prev = current;
current = current->next;
}
if (current == NULL)
{
head = current->next;
}
else
prev->next = current->next;
free(current);
printf("Node With value %d Deleted successfully.\n", key);
}
void searchNode(int key)
{
struct Node *current = head;
while (current != NULL)
{
if (current->data == key)
{
printf("Node With Value %d found.\n", key);
return;
}
current = current->next;
}
printf("Node With value %d Not Found.\n", key);
}
void traverseList()
{
struct Node *current = head;
printf("Linked List: ");
while (current != NULL)
{
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main()
{
int choice, value, key;
do
{
printf("\nMenu:\n1. InsertNode At first\n2. InsertNode At Last\n3. InsertNode At Random\n4. Delete At Front\n5. Delete At Last\n6. Delete At Random\n7. Search\n8. Traverse\n9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("Enter The Value To Insert: ");
scanf("%d", &value);
insertNodeFirst(value);
break;
}
case 2:
{
printf("Enter The Value To Insert: ");
scanf("%d", &value);
insertNodeLast(value);
break;
}
case 3:
{
printf("Enter The VAlue To Insert ? ");
scanf("%d", &value);
RandomInsert(value);
break;
}
case 4:
{
DeleteFront();
break;
}
case 5:
{
DeleteEnd();
break;
}
case 6:
{
printf("Enter The Value To Delete: ");
scanf("%d", &key);
DeleteRandom(key);
break;
}
case 7:
{
printf("Enter The Value To Search: ");
scanf("%d", &key);
searchNode(key);
break;
}
case 8:
{
traverseList();
break;
}
case 9:
{
printf("Exiting The Program\n");
exit(0);
}
default:
{
printf("Invalid Choice. Please Enter a Valid Option\n");
}
}
} while (1);
return 0;
}