/
DoubleLinkedList.cpp
314 lines (299 loc) · 7.2 KB
/
DoubleLinkedList.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright Vineet Kumar Srivastava
* This program was developed by Vineet Kumar Srivastava who is also the copyright owner.
* This code will be available as open source and can be used for teaching and self development process
* But this code cannot be used for professional purpose. In that case you can contact me for a licence.
* Date Created: 20th November 2010
*/
#include "DoubleLinkedList.h"
/* Main Routine */
void main()
{
struct dubLinkList *head= NULL,*temp=NULL;
int more=1,d=0,data=0, a = 0;
position pos = LIST_START;
printf("Do you want to create a Linked List: Enter 1 for YES 2 for NO:\n");
scanf("%d", &d);
if(1==d)
{
while(more)
{
printf("\nEnter a number:");
scanf("%d", &data);
if(NULL == head)
{
head=(dubLinkList *)calloc(1, sizeof(struct dubLinkList));
head->next=NULL;
head->data=data;
head->prev = NULL;
temp=head;
printf("Head of the Linked List created\n");
}
else
{
temp->next=(dubLinkList *)calloc(1, sizeof(struct dubLinkList));
temp->next->data=data;
temp->next->next=NULL;
temp->next->prev = temp;
temp=temp->next;
}
printf("\nDo you want to enter one more number: Enter 1 for YES 2 for NO:");
scanf("%d", &a);
if(1 == a)
more=1;
else
more=0;
}
printf("\nPrinting the contents of List\n");
printList(head);
more=1;
while(more)
{
printf("\n Enter one of the following choices \n 1. For Adding a number at the Head \
\n 2. For adding a number after another Number \
\n 3. For Adding a number at the Last \
\n 4. Deleting the head of list \
\n 5. Deleting a number from the List \
\n 6. Deleting the last number from the List \
\n 7. Print the contents of the list \
\n 8. For REVERSING the Linked List \n:");
scanf("%d", &d);
switch(d)
{
case 1:
pos=LIST_START;
addNode(&head, pos);
printList(head);
break;
case 2:
pos=LIST_AFTER_A_NUM;
addNode(&head, pos);
printList(head);
break;
case 3:
pos=LIST_END;
addNode(&head, pos);
printList(head);
break;
case 4:
pos=LIST_START;
delNode(&head, pos);
if(NULL != head)
{
printf("\nThe resulting linked list is ");
printList(head);
}
else
printf("\nThe resulting linked list is EMPTY");
break;
case 5:
pos=LIST_AFTER_A_NUM;
delNode(&head, pos);
if(NULL != head)
{
printf("\nThe resulting linked list is ");
printList(head);
}
else
printf("\nThe resulting linked list is EMPTY");
break;
case 6:
pos=LIST_END;
delNode(&head, pos);
if(NULL != head)
{
printf("\nThe resulting linked list is ");
printList(head);
}
else
printf("\nThe resulting linked list is EMPTY");
break;
case 7:
printList(head);
break;
case 8:
//reverseList(&head);
printf("\nThe resulting linked list is ");
printList(head);
break;
default:
printf("\n You have not entered a valid choice \n");
break;
}
printf("\nDo you want to do one more operation: Enter 1 for YES 2 for NO:");
scanf("%d", &a);
if(2 == a)
more=0;
else
more=1;
}
/* Free the linked list before exiting from the program */
if(head)
freeList(&head);
}
getche();
}
/* Function to print the contents of the Double Linked List */
void printList(struct dubLinkList *head)
{
struct dubLinkList *temp = head;
printf("\n The List is ");
while(temp)
{
printf(" %d ", temp->data);
temp = temp->next;
}
printf("\n");
}
/* Function to add a node in the Double Linked List at the position mentioned */
void addNode(struct dubLinkList **href, position pos)
{
/* Do the memory allocation to the new node first as it is a common operation */
int num = -1;
struct dubLinkList *temp = *href;
struct dubLinkList *newNode = (struct dubLinkList *)calloc(1, sizeof(struct dubLinkList));
printf("\n addNode: Enter the data:");
scanf("%d", &num);
newNode->data = num;
switch(pos)
{
case LIST_START:
{
newNode->next = *href;
if(NULL != *href)
{
(*href)->prev = newNode;
}
*href = newNode;
printf("\n addNode: Added node at the start of the Double Linked list");
}
break;
case LIST_AFTER_A_NUM:
{
int ref = -1;
printf("\n addNode: Enter the data after which you want to insert a data :");
scanf("%d", &ref);
while( temp && (ref != temp->data))
temp = temp->next;
if(NULL != temp)
{
printf("\n addNode: Key found, Inserting data ");
newNode->next = temp->next;
newNode->prev = temp;
temp->next = newNode;
printf("\n addNode: data inserted ");
}
else
{
printf("\n addNode: key not found in the list, please try again");
if(newNode)
{
free(newNode);
newNode = NULL;
}
}
}
break;
case LIST_END:
{
/* Move to the last node in the linked list */
while(temp->next)
{
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
printf("\n addNode: Added at the last in the Doubly Linked List");
}
break;
default:
{
printf("\n addNode: Not handled for %d value of pos", pos);
if(newNode)
{
free(newNode);
newNode = 0;
}
}
}
}
/* Function to del a node in the Double Linked List at the position mentioned */
void delNode(struct dubLinkList **href, position pos)
{
struct dubLinkList *temp = *href;
struct dubLinkList *nodeToBeDeleted = NULL;
if(NULL == *href)
{
printf("\n delNode: List is empty, hence returning ");
return;
}
switch(pos)
{
case LIST_START:
{
nodeToBeDeleted = *href;
*href = (*href)->next;
if(*href)
(*href)->prev = NULL;
free(nodeToBeDeleted);
nodeToBeDeleted = NULL;
}
break;
case LIST_AFTER_A_NUM:
{
int key = -1;
printf("\n delNode: Enter the key:");
scanf("%d", &key);
while(key != temp->data)
temp = temp->next;
if(NULL == temp)
{
printf("\n delNode: key not found ");
return;
}
else
{
printf("\n delNode: Key found ");
nodeToBeDeleted = temp;
if(temp->prev)
temp->prev->next = temp->next;
if(temp->next)
temp->next->prev = temp->prev;
if(temp == *href)
*href = NULL;
free(nodeToBeDeleted);
nodeToBeDeleted = NULL;
}
}
break;
case LIST_END:
{
while(temp->next)
temp = temp->next;
nodeToBeDeleted = temp;
if(temp->prev)
temp->prev->next = NULL;
else
*href = NULL;
}
break;
default:
printf("\n delNode: Unhandled value %d for pos ", pos);
}
}
/* Function to free the Double Linked List */
void freeList(struct dubLinkList **href)
{
struct dubLinkList *temp = *href;
struct dubLinkList *nodeToBeFreed = NULL;
while(temp)
{
nodeToBeFreed = temp;
temp = temp->next;
*href = temp;
free(nodeToBeFreed);
nodeToBeFreed = NULL;
}
printf("\n freeList: All the nodes in the Double Linked List are freed ");
}
/* End of File */