-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedList.c
272 lines (243 loc) · 6.82 KB
/
DoubleLinkedList.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int number;
struct node *next;
}*head,*newNode,*tail,*searchptr,*pointer, *after, *before;
//after: To find the node to insert / delete after. That is: before = searchptr->next;
//before: To find the node to insert / delete before. That is after = searchptr->prev;
//newNode: Pointer to the new node created
//searchptr: Pointer having the address of the node whose number is searched
//pointer: Used in print function to start printing from the head
//tail: Always points to last node. tail->next is NULL
//head: Always points to beginning. head->prev = NULL
int count = 0;//To count the number of nodes created
/* Generic function to create a new node */
void createNewNode()
{
int data;
newNode =(struct node *)malloc(1*sizeof(struct node));
newNode->prev = NULL;
newNode->next = NULL;
printf("\n Enter number to create new node : ");
scanf("%d", &data);
newNode->number = data;
count++;
}
/* TO insert at beginning */
void insertBegin()
{
if (head == NULL)
{
createNewNode();
head = newNode;
tail = head;
}
else
{
createNewNode();
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}//end insertBegin
/* To insert at end */
void insertEnd()
{
if (head == NULL)
{
createNewNode();
head = newNode;
tail = head;
}
else
{
createNewNode();
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}//end insertEnd
/* Function to insert before a number searched */
void insertBefore()
{
after = searchptr->prev;
if(searchptr == head)
{
printf("\n Inserting before the head node...");
insertBegin();
}
else
{
if(searchptr == tail) printf("\n Inserting before tail node...");
else printf("\n Inserting after %d and before %d", after->number, searchptr->number);
createNewNode();
searchptr->prev = newNode;
newNode->next = searchptr;
after->next = newNode;
newNode->prev = after;
}
}//end insertBefore
/* Function to insert after a number searched */
void insertAfter()
{
before = searchptr->next;
if(searchptr == tail)
{
printf("\n Inserting after the tail node...");
insertEnd();
}
else
{
if(searchptr == head) printf("\n Inserting after head node...");
else printf("\n Inserting after %d and before %d",searchptr->number, before->number);
createNewNode();
searchptr->next = newNode;
newNode->prev = searchptr;
before->prev = newNode;
newNode->next = before;
}
}//end insertBefore
/* Print from beginning */
void printAll()
{
pointer = head;
if (pointer == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
while (pointer->next != NULL)
{
printf(" %d ", pointer->number);
pointer = pointer->next;
}
printf(" %d ", pointer->number);
}//end printAll
/* Search for a number*/
struct node * mysearch(int data)
{
int position = 0;
searchptr = head;
if (searchptr == NULL)
{
printf("\n Error : List empty to search for data");
return NULL;
}
while (searchptr != NULL)
{
if (searchptr->number == data)
{
printf("\n Data found in %d position ",position + 1);
return searchptr;
}
else
searchptr = searchptr->next;
position++;
}
return NULL;
}//end mysearch
/* Function to
--- delete node at beginning
--- delete node for a given number
--- delete node at end
*/
void deleteNode()
{
pointer = head;
if(searchptr == head)
{
head = searchptr->next;
free(searchptr);
return;
}
if(searchptr == tail)
{
tail = searchptr->prev;
tail->next = NULL;
free(searchptr);
return;
}
before = searchptr->next;
after = searchptr->prev;
before->prev = after->next;
after->next = before;
free(searchptr);
return;
}//end deleteNode
int main()
{
int ch;
head = NULL;
newNode = NULL;
tail = NULL;
searchptr = NULL;
int data;
printf("\n 1 - Insert at beginning of doubly linked list");
printf("\n 2 - Insert at end of doubly linked list");
printf("\n 3 - Insert before a number of doubly linked list");
printf("\n 4 - Insert after a number of doubly linked list");
printf("\n 5 - Search for a number in doubly linked list");
printf("\n 6 - Delete a number in doubly linked list");
printf("\n 7 - Display the doubly linked list");
printf("\n -1 - Quit");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insertBegin();printAll();
break;
case 2:
insertEnd();printAll();
break;
case 3:printf("\n Enter number to insert before : ");
scanf("%d", &data);
searchptr = mysearch(data);
if(searchptr != NULL)
{
insertBefore();printAll();
}
else printf("\n Number %d not found to insert before!!", data );
break;
case 4:
printf("\n Enter number to insert after : ");
scanf("%d", &data);
searchptr = mysearch(data);
if(searchptr != NULL)
{
insertAfter();printAll();
}
else printf("\n Number %d not found to insert after!!", data );
break;
case 5:printf("\n Enter element to search : ");
scanf("%d", &data);
searchptr = mysearch(data);
if(searchptr == NULL) printf("\n Number %d not found!!", data );
else printf("\n %d number found!!",searchptr->number );
break;
case 6:
printf("\n Enter number to delete: ");
scanf("%d", &data);
searchptr = mysearch(data);
if(searchptr != NULL)
{
deleteNode();printAll();
}
else printf("\n Number %d not found to delete!!", data );
break;
case 7:
printAll();
break;
case -1:
exit(0);
default:
printf("\n Wrong choice menu");
}//end switch
}//end while
}//end main