-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinkedListInsertionDeletion.c
196 lines (185 loc) · 3.92 KB
/
linkedListInsertionDeletion.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL, *newNode, *temp;
int count = 0;
void display()
{
if (head == NULL)
{
printf("\nList is empty\n");
}
else
{
temp = head;
printf("\nLinked List : ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
count++;
}
}
}
void insertAtBeginning()
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert : ");
scanf("%d", &newNode->data);
newNode->next = head;
head = newNode;
}
void insertAtEnd()
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert : ");
scanf("%d", &newNode->data);
newNode->next = NULL;
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
void insertAtPos()
{
int pos, i = 1;
printf("\nEnter the position to insert at : ");
scanf("%d", &pos);
if (pos > count)
{
printf("\nInvalid position!");
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert : ");
scanf("%d", &newNode->data);
newNode->next = NULL;
temp = head;
while (i < pos)
{
temp = temp->next;
i++;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
void deleteFromBeginning()
{
if (head == NULL)
{
printf("\nList empty");
}
else
{
temp = head;
head = head->next;
free(temp);
}
}
void deletefromEnd()
{
struct node *prevnode;
temp = head;
while (temp->next != NULL)
{
prevnode = temp;
temp = temp->next;
}
if (temp == head)
{
head = NULL;
}
else
{
prevnode->next = NULL;
}
free(temp);
}
void deletefromPos()
{
struct node *nextnode;
int pos, i = 1;
temp = head;
printf("\nEnter the position to delete from : ");
scanf("%d", &pos);
while (i < pos - 1)
{
temp = temp->next;
i++;
}
nextnode = temp->next;
temp->next = nextnode->next;
free(nextnode);
}
void reverse()
{
struct node *prevnode, *currentnode, *nextnode;
prevnode = NULL;
currentnode = nextnode = head;
while (nextnode != NULL)
{
nextnode = nextnode->next;
currentnode->next = prevnode;
prevnode = currentnode;
currentnode = nextnode;
}
head = prevnode;
}
void main()
{
int ch;
do
{
printf("\n\nChoose operation");
printf("\n1.Display");
printf("\n2.Insert at Beginning");
printf("\n3.Insert at End");
printf("\n4.Insert at Specified Position");
printf("\n5.Delete from Beginning");
printf("\n6.Delete from End");
printf("\n7.Delete from a Specified Position");
printf("\n8.Reverse");
printf("\n9.Exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
display();
break;
case 2:
insertAtBeginning();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPos();
break;
case 5:
deleteFromBeginning();
break;
case 6:
deletefromEnd();
break;
case 7:
deletefromPos();
break;
case 8:
reverse();
break;
case 9:
break;
default:
printf("Invalid Choice!");
break;
}
} while (ch < 9);
}