-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathDoublyLinkedList.c
248 lines (213 loc) · 4.46 KB
/
DoublyLinkedList.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
// C program for the all operations in the Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Structure of a Doubly Linked List
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node* head = NULL;
// Traversal of a Doubly Linked List
void traversal()
{
// if head of linked list is NULL or the linked list is empty
if (head == NULL) {
printf("\nList is empty\n");
return;
}
// Otherwise print the data by traversing the linked list
struct node* temp;
temp = head;
while (temp != NULL) {
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
// Function to insert an element at the front of the Doubly Linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->prev = NULL;
temp->next = head;
head = temp;
}
// Function to insert an element at the end of the Doubly Linked list
void insertAtEnd()
{
int data;
struct node *temp, *ptr;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
ptr = head;
if (head == NULL) {
head = temp;
}
else {
while (ptr->next != NULL)
ptr = ptr->next;
temp->prev = ptr;
ptr->next = temp;
}
}
// Function to insert an element at a given index of the Doubly Linked list
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
printf("\nEnter position : ");
scanf("%d", &pos);
if (head == NULL) {
head = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}else if (pos == 1) {
insertAtFront();
}else {
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
newnode->data = data;
temp = head;
while (i < pos - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
// Function to delete front element of the Doubly Linked list
void deleteFirst()
{
struct node* temp;
if (head == NULL)
printf("\nList is empty\n");
else {
temp = head;
head = temp->next;
if (head != NULL)
head->prev = NULL;
free(temp);
}
}
// Function to delete last element of the Doubly Linked list
void deleteEnd()
{
struct node* temp;
if (head == NULL)
printf("\nList is empty\n");
temp = head;
while (temp->next != NULL)
temp = temp->next;
if (head->next == NULL)
head = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
// Function to delete an element of a given index of the Doubly Linked list
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = head;
if (head == NULL)
printf("\nList is empty\n");
else {
printf("\nEnter position : ");
scanf("%d", &pos);
if (pos == 1) {
deleteFirst();
if (head != NULL) {
head->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1) {
temp = temp->next;
i++;
}
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{
int choice;
while (1) {
// Choose t1 to traverse and print the list
// Choose t2 to insert an element at the front
// Choose t3 to insert an element at the end
// Choose t4 to insert an element at a given position
// Choose t5 to delete the first element
// Choose t6 to delete the last element
// Choose t7 to delete an element of a given position
// Choose t8 to exit the program
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n");
printf("\t3 For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n");
printf("\t6 For deletion of "
"last element\n");
printf("\t7 For deletion of "
"element at any position\n");
printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traversal();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Wrong Choice \n");
continue;
}
}
return 0;
}