This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
290 lines (243 loc) · 4.61 KB
/
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
/*
*
* list.c
*
* Baltasar Dinis 89416
* IAED project
*
* defines the list datatype
* defines the auxiliar datatype l_node
*/
#include "list.h"
/*-------------------------------*/
/* prototypes */
/*-------------------------------*/
/*-------------------------------*/
/*-------------------------------*/
/*-------------------------------*/
/*
* function: l_node_
*
* construct a l_node
* val: value (l_item)
* prev: ptr to preceding node
* next: ptr to the next node
*
* return: ptr to l_node
*/
l_node *l_node_(l_item val, l_node *prev, l_node *next)
{
l_node *n = (l_node *) malloc(sizeof(l_node));
val(*n) = val;
prev(*n) = prev;
next(*n) = next;
return n;
}
/*
* function: is_head
*
* checks if a node is a head of a linked list
* n: ptr to node
*
* return: true if n is head
*/
bool is_head(l_node *n)
{
return (n != NULL && prev(*n) == NULL);
}
/*
* function: is_tail
*
* checks if a node is a tail of a linked list
* n: node
*
* return: true if n is tail
*/
bool is_tail(l_node *n)
{
return (n != NULL && next(*n) == NULL);
}
/*
* function: go_next
*
* advances to the next node in the linked list
* n: node
*
* return: next node
* if the node is a tail, return itself
*/
l_node *go_next(l_node *n)
{
return (next(*n) == NULL) ? n : next(*n);
}
/*
* function: go_prev
*
* returns to the previous node in the linked list
* n: node
*
* return: next node
* if the node is a head, return itself
*/
l_node *go_prev(l_node *n)
{
return (prev(*n) == NULL) ? n : prev(*n);
}
/*
* function: insert_l_node_after
*
* inserts a node after the src
* src: ptr to node
* val: value of the node to be inserted
*
* return: false if the src is a tail
*/
bool insert_l_node_after(l_node *src, l_item val)
{
l_node *new_node;
if (is_tail(src))
return false;
new_node = l_node_(val, src, next(*src));
next(*src) = new_node;
prev(*next(*new_node)) = new_node;
return true;
}
/*
* function: insert_l_node_before
*
* inserts a node before the src
* src: ptr to node
* new_node: ptr to node to be inserted
*
* return: false if the src is a head
*/
bool insert_l_node_before(l_node *src, l_item val)
{
l_node *new_node;
if (is_head(src))
return false;
new_node = l_node_(val, prev(*src), src);
prev(*src) = new_node;
next(*prev(*new_node)) = new_node;
return true;
}
/*
* function: remove_l_node
*
* removes a l_node
* src: ptr to node
*
* return: false if the src is a head or tail
*/
bool remove_l_node(l_node *src)
{
if (is_head(src) || is_tail(src))
return false;
next(*prev(*src)) = next(*src);
prev(*next(*src)) = prev(*src);
free_l_node(src);
return true;
}
/*
* function: free_l_node
*
* free the memory allocated for a node
* n: ptr to node
*/
void free_l_node(l_node *n)
{
if (n != NULL) {
free_l_item(val(*n));
free(n);
n = NULL;
}
}
/*
* function: careless_free_l_node
*
* carelessly free the memory allocated for a node
* ONLY USE WHEN FREEING EVERYTHING
* n: ptr to node
*/
void careless_free_l_node(l_node *n)
{
if (n != NULL) {
careless_free_l_item(val(*n));
free(n);
n = NULL;
}
}
/*
* function: lnkd_list_
*
* constructor for lnkd_list
* initializes a linked list
* l: ptr to lnkd_list
*
*/
lnkd_list *lnkd_list_()
{
lnkd_list *l = (lnkd_list *) malloc(sizeof(lnkd_list));
head(*l) = (l_node *) malloc(sizeof(l_node));
tail(*l) = (l_node *) malloc(sizeof(l_node));
next(*head(*l)) = tail(*l);
prev(*tail(*l)) = head(*l);
prev(*head(*l)) = next(*tail(*l)) = NULL;
return l;
}
/*
* function: empty_list
*
* test for the lnkd_list datatype
* check if a list is empty
*
* return: true if the list is empty
*/
bool empty_list(lnkd_list *l)
{
return is_tail( next(*head(*l)) );
}
/*
* function: add_at_beginning
*
* add an element at the beginning of the linked list
* l: ptr to list
* val: l_item
*/
void add_at_beginning(lnkd_list *l, l_item val)
{
insert_l_node_after(head(*l), val);
}
/*
* function: add_at_end
*
* add an element at the end of the linked list
* l: ptr to list
* val: l_item
*/
void add_at_end(lnkd_list *l, l_item val)
{
insert_l_node_before(tail(*l), val);
}
/*
* function: free_lnkd_list
*
* frees a list (and all its elements)
* l: list
*/
void free_lnkd_list(lnkd_list *l)
{
l_node *n;
if (l != NULL) {
n = next(*next(*head(*l)));
while (n != NULL && !is_tail(n)) {
careless_free_l_node(prev(*n));
n = go_next(n);
}
careless_free_l_node(prev(*tail(*l)));
free(head(*l));
free(tail(*l));
free(l);
l = NULL;
}
}