-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
doubly_linked_list.c
293 lines (262 loc) · 7.2 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
/**
* @file
* @brief Implementation of [Doubly linked list](https://en.wikipedia.org/wiki/Doubly_linked_list)
* @details
* A doubly linked list is a data structure with a sequence
* of components called nodes. Within that nodes there are
* three elements: a value recorded, a pointer to the next
* node, and a pointer to the previous node.
*
* In this implementation, the functions of creating the list,
* inserting by position, deleting by position, searching
* for value, printing the list, and an example of how the
* list works were coded.
*
* @author [Gabriel Mota Bromonschenkel Lima](https://github.com/GabrielMotaBLima)
*/
#include <stdio.h>
#include <stdlib.h>
/**
* @brief Doubly linked list struct
*/
typedef struct list
{
double value; ///< value saved on each node
struct list *next, *prev; ///< directing to other nodes or NULL
} List;
/**
* @brief Create list function, a new list containing one node will be created
* @param value a value to be saved into the first list node
* @returns new_list the list created
*/
List *create(double value);
/**
* @brief Insertion by position into the list function
* @param list a doubly linked List
* @param value a value to be inserted into the list
* @param pos a position into the list for value insertion
* @returns list the input list with a node more or the same list
*/
List *insert(List *list, double value, int pos);
/**
* @brief Deletion by position into the list function
* @param list a doubly linked List
* @param pos a position into the list for value Deletion
* @returns list the input list with deleted values or the same list
*/
List *delete(List *list, int pos);
/**
* @brief Search value into the list function
* @param list a doubly linked list
* @param value a value to be looked for into the list
* @returns `1` if the looked up value exists
* @returns `0` if the looked up value doesn't exist
*/
int search(List *list, double value);
/**
* @brief Print list function
* @param list a doubly linked List
* @returns void
*/
void print(List *list);
/**
* @brief Example function
* @returns void
*/
void example();
/**
* @brief Main function
* @returns 0 on exit
*/
int main()
{
// examples for better understanding
example();
// code here
return 0;
}
/**
* @brief Create list function, a new list containing one node will be created
* @param value a value to be saved into the first list node
* @returns new_list the list created
*/
List *create(double value)
{
List *new_list = (List *)malloc(sizeof(List));
new_list->value = value;
new_list->next = NULL;
new_list->prev = NULL;
return new_list;
}
/**
* @brief Insertion by position into the list function
* @param list a doubly linked List
* @param value a value to be inserted into the list
* @param pos a position into the list for value insertion
* @returns list the input list with a node more or the same list
*/
List *insert(List *list, double value, int pos)
{
// list NULL case
if (list == NULL)
{
list = create(value);
return list;
}
// position existing case
if (pos > 0)
{
List *cpy = list, *tmp = cpy;
int flag = 1, index = 1, size = 0;
while (tmp != NULL)
{
size++;
tmp = tmp->next;
}
// first position case
if (pos == 1)
{
List *new_node = create(value);
new_node->next = cpy;
cpy->prev = new_node;
list = new_node;
return list;
}
// position existing in list range case
if (size + 2 > pos)
{
while (cpy->next != NULL && index < pos)
{
flag++;
index++;
cpy = cpy->next;
}
List *new_node = (List *)malloc(sizeof(List));
new_node->value = value;
// position into list with no poiting for NULL
if (flag == pos)
{
cpy->prev->next = new_node;
new_node->next = cpy;
new_node->prev = cpy->prev;
cpy->prev = new_node;
}
// last position case
if (flag < pos)
{
new_node->next = cpy->next;
new_node->prev = cpy;
cpy->next = new_node;
}
}
return list;
}
}
/**
* @brief Deletion by position into the list function
* @param list a doubly linked List
* @param pos a position into the list for value Deletion
* @returns list the input list with deleted values or the same list
*/
List *delete(List *list, int pos)
{
// list NULL case
if (list == NULL)
return list;
// position existing case
if (pos > 0)
{
List *cpy = list, *tmp = cpy;
int flag = 1, index = 1, size = 0;
while (tmp != NULL)
{
size++;
tmp = tmp->next;
}
// first position case
if (pos == 1)
{
if (size == 1)
return NULL;
cpy = cpy->next;
cpy->prev = NULL;
return cpy;
}
// position existing in list range case
if (size + 2 > pos)
{
while (cpy->next != NULL && index < pos)
{
flag++;
index++;
cpy = cpy->next;
}
if (flag == pos)
{
// position into list with no poiting for NULL
if (cpy->next != NULL)
{
cpy->prev->next = cpy->next;
cpy->next->prev = cpy->prev;
}
// last position case
else
cpy->prev->next = NULL;
}
}
return list;
}
}
/**
* @brief Search value into the list function
* @param list a doubly linked list
* @param value a value to be looked for into the list
* @returns `1` if the looked up value exists
* @returns `0` if the looked up value doesn't exist
*/
int search(List *list, double value)
{
if (list == NULL)
return 0;
if (list->value == value)
return 1;
search(list->next, value);
}
/**
* @brief Print list function
* @param list a doubly linked List
* @returns void
*/
void print(List *list)
{
if (list != NULL)
{
printf("%f\t", list->value);
print(list->next);
}
}
/**
* @brief Example function
* @returns void
*/
void example()
{
List *my_list = NULL;
double node_value = 0;
int searching;
my_list = create(node_value);
my_list = insert(my_list, 3, 1);
my_list = insert(my_list, 5, 3);
my_list = insert(my_list, 10, 3);
my_list = insert(my_list, 20, 3);
print(my_list);
searching = search(my_list, 20);
printf("\n%d\n", searching);
my_list = delete (my_list, 1);
my_list = delete (my_list, 1);
my_list = delete (my_list, 1);
my_list = delete (my_list, 1);
print(my_list);
searching = search(my_list, 20);
printf("\n%d\n", searching);
}