-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_operations_on_node.c
More file actions
291 lines (260 loc) · 7.98 KB
/
Linked_List_operations_on_node.c
File metadata and controls
291 lines (260 loc) · 7.98 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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *link;
};
void show(struct Node* node)
{
if(node == NULL)
printf("Empty!");
else {
printf("|");
while (node != NULL) {
printf(" %d |", node->data);
node = node->link;
}
}
}
void push(struct Node** head_ref, int new_data)
{
/*Allocation of New Node*/
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/*Adding Data to the New Node*/
new_node -> data = new_data;
/*Converting the new_node as Head*/
new_node -> link = *head_ref;
/*Moving the Head-Node a step back*/
*head_ref = new_node;
printf("[+] Successfully Added\n");
}
void append(struct Node** head_ref, int new_data)
{
/*New-Node Creation*/
struct Node* new_node = (struct Node*)malloc(sizeof (struct Node));
/*Adding Data to the New Node*/
new_node -> data = new_data;
/*The address section of the new_node is set to null*/
new_node -> link = NULL;
/*If the list is empty, th new_node is declared as the new_node*/
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/*Else traverse from the head to the end and add the new_node*/
struct Node* last = *head_ref;
while(last -> link != NULL)
last = last -> link;
last -> link = new_node;
printf("[+] Successfully Added\n");
}
void add_after_head(struct Node** head_ref, struct Node* head, int data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* temp_node = (struct Node*)malloc(sizeof(struct Node));
new_node -> data = data;
new_node->link = NULL;
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
temp_node -> link = head->link;
head -> link = new_node;
new_node -> link = temp_node->link;
printf("[+] Successfully Added\n");
}
void add_after_required(struct Node* node, int data, int user_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
int c=0;
while(node!=NULL) {
if(node->data == data) {
new_node->data = user_data;
new_node->link = node->link;
node->link = new_node;
printf("[+] Successfully Added\n");
c+=1;
break;
}
node = node->link;
}
if(c==0)
printf("[-] Operation unsuccessful!\n");
}
void pop(struct Node** head)
{
if(*head == NULL) {
printf("Underflow has occurred!\n");
}
else
{
printf("Data popped: %d\n", (*head)->data);
*head = (*head) -> link;
}
}
void deleteAlt(struct Node *head)
{
if (head == NULL) {
printf("[-] Operation not possible!");
return;
}
else if(head -> link == NULL) {
printf("[-] Operation not possible!");
return;
}
/* Initialize prev and node to be deleted */
struct Node *prev = head;
struct Node *node = head->link;
while (prev != NULL && node != NULL)
{
/* Change next link of previous node */
prev->link = node->link;
/* Update prev and node */
prev = prev->link;
if (prev != NULL)
node = prev->link;
}
free(node);
printf("[+] Deleted alternate nodes\n");
printf("The current state of the List: ");
show(head);
}
void LastNodeDeletion(struct Node *head, struct Node** head_ref)
{
struct Node *toDelLast, *preNode = NULL;
if(head == NULL)
show(head);
else if(head->link == NULL)
{
struct Node* new_head = NULL;
*head_ref = new_head;
}
else
{
toDelLast = head;
preNode = head;
/* Traverse to the last node of the list*/
while(toDelLast->link != NULL)
{
preNode = toDelLast;
toDelLast = toDelLast->link;
}
if(toDelLast == head)
{
head = NULL;
}
else
/* Disconnects the link of second last node with last node */
preNode->link = NULL;
}
}
void delete_a_particular_Node(struct Node** head_ref, int key)
{
// Store head node
struct Node *temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == key) {
*head_ref = temp->link; // Changed head
free(temp); // free old head
printf("[+] Success!\n");
printf("The List after deletion of the node: ");
show(*head_ref);
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->link;
}
// If key was not present in linked list
if (temp == NULL) {
printf("[-] No such value was found to be deleted!");
return;
}
// Unlink the node from linked list
prev->link = temp->link;
printf("[+] Success!\n");
printf("The List after deletion of the node: ");
show(*head_ref);
free(temp); // Free memory
}
int main(void) {
struct Node *head = NULL;
printf("Welcome to the Linked List Test!\n");
printf("--------------------------------\n--------------------------------\n");
printf("Press:\n1. To Push integers at the beginning of the list\n2. To Append integers as the end of the list\n3. To Add integers after the head\n4. To Add integers after any other data\n5. To pop data (POP means to remove data from the beginning of the list, i.e, to delete the very 1st node)\n6. To delete alternate nodes \n7. To delete data from the end\n8. To delete a particular Node\n9. To show the List\n10. Exit");
int choice;
do
{
int data;
printf("\nEnter your choice:\n-->");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the number to be pushed:\n-->");
scanf("%d", &data);
push(&head, data);
printf("The current state of the List: ");
show(head);
break;
case 2:
printf("Enter the number to be appended:\n-->");
scanf("%d", &data);
append(&head, data);
printf("The current state of the List: ");
show(head);
break;
case 3:
printf("Enter the number to be added after the very 1st node:\n-->");
scanf("%d", &data);
add_after_head(&head, head, data);
printf("The current state of the List: ");
show(head);
break;
case 4:
printf("The current status of the LIST: ");
show(head);
printf("\nEnter the number to be added:\n-->");
scanf("%d", &data);
int given_data;
printf("Enter the number after which you want to insert the number:\n-->");
scanf("%d", &given_data);
add_after_required(head, given_data, data);
break;
case 5:
pop(&head);
printf("The current state of the List: ");
show(head);
break;
case 6:
deleteAlt(head);
break;
case 7:
LastNodeDeletion(head, &head);
printf("The current state of the List: ");
show(head);
break;
case 8:
printf("Enter the value to be deleted:---> ");
scanf("%d", &data);
delete_a_particular_Node(&head, data);
break;
case 9:
printf("The LIST:\n");
show(head);
break;
case 10:
free(head);
printf("[+] Process Ended with code 0");
exit(0);
default:
printf("Wrong choice!");
}
}
while(choice!=10);
}