-
Notifications
You must be signed in to change notification settings - Fork 5
/
doubly_linked_list.c
345 lines (278 loc) · 5.62 KB
/
doubly_linked_list.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct DOUBLY_LINKED_LIST
{
int data;
struct DOUBLY_LINKED_LIST *next;
struct DOUBLY_LINKED_LIST *previous;
} List;
List *front = NULL;
/**
* Creates a new node.
*
* @internal
*
* @param int data Data for node
*
* @return Node
*/
List *get_node(int data)
{
List *temp = (List *) malloc(sizeof(List));
temp->data = data;
temp->next = NULL;
temp->previous = NULL;
return temp;
}
/**
* Insert in front of the list.
*
* @param List list
* @param List new_node Node to insert
*
* @return List
*/
List *insert_first(List *list, List *new_node)
{
new_node->next = list;
list->previous = new_node;
front = new_node;
return new_node;
}
/**
* Insert at the end of list.
*
* @param List list
* @param List new_node Node to insert
*
* @return List
*/
List *insert_last(List *list, List *new_node)
{
if (list->next) {
insert_last(list->next, new_node);
} else {
list->next = new_node;
new_node->previous = list;
}
return list;
}
/**
* Insert in between 2 nodes.
*
* @param List list
* @param List new_node Node to insert
*
* @return
*/
List *insert_between(List *list, List *new_node)
{
static List *parent;
if (!list) {
return list;
}
if (list->data > new_node->data) {
parent->next = new_node;
new_node->previous = parent;
new_node->next = list;
list->previous = new_node;
} else {
parent = list;
insert_between(list->next, new_node);
}
return list;
}
/**
* Find position to insert a given node.
*
* @param List list
* @param new_node
*
* @return List
*/
List *find_insert_position(List *list, List *new_node)
{
static List *parent;
if (NULL == parent) {
parent = list;
}
if (new_node->data > list->data) {
parent = list;
if (list->next) {
find_insert_position(list->next, new_node);
}
}
return parent;
}
/**
* Find node in a list.
*
* @param List list
* @param Int data Data to search
*
* @return List
*/
List *find(List *list, int data)
{
if (!list) {
return NULL;
}
if (data == list->data) {
return list;
}
return find(list->next, data);
}
/**
* Insert new node to a sorted list.
*
* @param List
* @param int data Data to insert
*
* @return void
*/
void insert_sorted(List **list, int data)
{
List *new_node = get_node(data);
if (!*list) {
*list = new_node;
front = *list;
return;
}
List *parent = find_insert_position(*list, new_node);
if (!parent->next && new_node->data < parent->data) {
*list = insert_first(*list, new_node);
} else if (!parent->next && new_node->data > parent->data) {
insert_last(*list, new_node);
} else {
*list = insert_between(*list, new_node);
}
}
/**
* Insert new node to a list.
*
* @param List
* @param int data Data to insert
* @param int poistion Start, end or inbetween
*
* @return void
*/
void insert(List **list, int data, int position)
{
List *new_node = get_node(data);
if (!*list) {
*list = new_node;
front = *list;
return;
}
switch (position) {
case 0:
*list = insert_first(*list, new_node);
break;
case 1:
*list = insert_between(*list, new_node);
break;
case 2:
*list = insert_last(*list, new_node);
break;
}
}
/**
* Delete given node from a list.
*
* @param List list
* @param Int data Data to delete
*
* @return List|NULL
*/
List *delete(List **list, int data)
{
if (!*list) {
return NULL;
}
if (front->data == data) {
List *deleted_node = front;
front = front->next;
*list = front;
(*list)->previous = NULL;
return deleted_node;
}
List *node = find(*list, data);
if (!node->previous) {
return NULL;
}
List *delete_node = node;
node->previous->next = delete_node->next;
if (delete_node->next) {
delete_node->next->previous = delete_node->previous;
}
return delete_node;
}
/**
* Print a given list.
*
* @param List
*
* @return void
*/
void print_list(List *list)
{
if (!list) {
return;
}
printf("%d -> ", list->data);
print_list(list->next);
}
/**
* Print a given list in inverted order.
*
* @param List
*
* @return void
*/
void print_inverted(List *list)
{
if (!list) {
return;
}
print_inverted(list->next);
printf("%d -> ", list->data);
}
int main()
{
int data_array[] = {3, 2, 8, 6, 4, 4, 12};
List *l1 = NULL;
List *l2 = NULL;
insert(&l1, 3, 0); //root node
insert(&l1, 2, 0); //insert at start
insert(&l1, 8, 2); //insert at end
insert(&l1, 6, 1); //insert between 2 and 8
assert(front == l1);
print_list(l1);
free(l1);
for (int i = 0; i < 7; i++) {
insert_sorted(&l2, data_array[i]);
}
assert(front == l2);
printf("\n\n");
print_list(l2);
printf("\n");
print_inverted(l2);
List *find_node = find(l2, 8);
assert(8 == find_node->data);
find_node = find(l2, 99);
assert(NULL == find_node);
List *deleted_node = delete(&l2, 2);
assert(2 == deleted_node->data);
assert(front == deleted_node->next);
printf("\n\n");
print_list(l2);
deleted_node = delete(&l2, 4);
assert(4 == deleted_node->data);
printf("\n");
print_list(l2);
deleted_node = delete(&l2, 12);
assert(12 == deleted_node->data);
free(l2);
return 0;
}