-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsertion_del_linked_list.c
81 lines (69 loc) · 1.65 KB
/
insertion_del_linked_list.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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insertEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void deleteEnd() {
if (head == NULL) {
return; // Nothing to delete
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
struct Node* current = head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
}
void display() {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
int operation, value;
while (1) {
scanf("%d", &operation);
if (operation == -1) {
scanf("%d", &value);
if (value >= 0) {
insertEnd(value);
} else {
printf("-ve Node data\n");
break;
}
} else if (operation == -2) {
deleteEnd();
} else if (operation == -3) {
display();
} else if (operation == -4) {
break;
}
}
return 0;
}