-
Notifications
You must be signed in to change notification settings - Fork 26
/
InsertionDeletionLinkedList.c
163 lines (152 loc) · 5.13 KB
/
InsertionDeletionLinkedList.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
// A menu-driven C program which let's the user Insert , Delete , Display elements in list at different positions and situations.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
// Structure used to create node again and again when required..
struct node *CreateNode() {
struct node *new = (struct node*) malloc(sizeof(struct node));
return new;
}
void InsertAtBegin(int value) {
struct node *NewNode = CreateNode();
if (head == NULL) { /*Only works when list is empty*/
NewNode->data = value;
head = NewNode;
NewNode->next = NULL;
} else {
printf("\n\t**Element already exists at this position**\n");
}
}
void InsertAtnthNode(int pos , int value) {
struct node* temp = head;
if(pos==1) {
printf("\n\t**Use Insert at begining**\n");
} else {
struct node *NewNode = CreateNode();
NewNode->data = value;
NewNode->next = NULL;
for (int i=0; i<pos-2; i++) {
temp = temp->next; /*Accessing (n-1)th node*/
}
NewNode->next = temp->next; /*Linking nth node to (n+1)th node*/
temp->next = NewNode; /*Linking (n-1)th node to nth node*/
}
}
void InsertAtEnd(int value) {
if (head == NULL) { /*Does not work when list is empty. Underflow situation...*/
printf("\n\t**Use Insert at begining**\n");
} else {
struct node *temp = head;
while(temp->next!=NULL) {
temp = temp->next;
}
struct node *NewNode = CreateNode();
NewNode->data = value;
NewNode->next = temp->next;
temp->next = NewNode; /*Links new node n to (n-1)th node*/
}
}
void DeleteAtBegin() {
if (head == NULL) { /*Does not work when list is empty. Underflow situation...*/
printf("\n\t**No element exists**\n");
} else {
head = head->next; /*2nd node is now declared as head*/
printf("\n\t**Element deleted successfully**\n");
}
}
void DeleteAtEnd() {
if (head == NULL) { /*Does not work when list is empty. Underflow situation...*/
printf("\n\t**No element exists**\n");
} else if (head->next == NULL) {
printf("\n\t**Use Delete at begining**\n");
} else {
struct node *temp = head;
while(temp->next->next!=NULL) { /*Accessing (n-1)th node*/
temp = temp->next;
}
temp->next = NULL; /*(n-1)th node will now point to null instead of nth node*/
free(temp->next);
printf("\n\t**Element deleted successfully**\n");
}
}
void DeletenthNode(int pos) {
struct node *temp = head;
if (pos == 1) {
printf("\n\t**Use Delete at begining**\n");
} else {
for (int i=0; i<pos-2; i++) {
temp = temp->next;
}
struct node *temp2 = temp->next; /*Accessing nth node, which we want to delete*/
temp->next = temp2->next; /*(n-1) node is pointing to (n+1) node now. Breaking the link between (n-1),n,(n+1) nodes.*/
free(temp2);
printf("\n\t**Element deleted successfully**\n");
}
}
void Display() {
if (head == NULL) {
printf("\n\t**No elements to display**\n\n");
} else {
struct node *temp = head;
printf("\nCurrent List:\n");
while(temp!=NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
}
}
void main() {
head = NULL;
int ch;
while (1) {
printf("\n\t\t**MENU**\n\t1. Insert at begining\n\t2. Insert at nth position\n\t3. Insert at end\n\t4. Delete at begining\n\t5. Delete at end\n\t6. Delete nth node\n\t7. Display\n\t8. Exit\n");
printf("\n\tEnter your choice: ");
scanf("%d",&ch);
switch (ch) {
case 1:
printf("\nEnter value to be inserted: ");
int v1;
scanf("%d",&v1);
InsertAtBegin(v1);
break;
case 2:
printf("\nEnter position to insert value: ");
int v2 , pos1;
scanf("%d",&pos1);
printf("Enter value to be inserted: ");
scanf("%d",&v2);
InsertAtnthNode(pos1 , v2);
break;
case 3:
printf("\nEnter value to insert at end: ");
int v3;
scanf("%d",&v3);
InsertAtEnd(v3);
break;
case 4:
DeleteAtBegin();
break;
case 5:
DeleteAtEnd();
break;
case 6:
printf("\nEnter position to delete element: ");
int pos2;
scanf("%d",&pos2);
DeletenthNode(pos2);
break;
case 7:
Display();
break;
case 8:
printf("\n\t**THANK YOU!**\n");
exit(0);
default:
printf("\n\t**Chose a valid option**\n");
}
}
}