-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
277 lines (233 loc) · 5.66 KB
/
list.c
File metadata and controls
277 lines (233 loc) · 5.66 KB
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
/*
Linked list datatype
Copyright (C) 2017 Erik Scharwaechter <erik.scharwaechter@hpi.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "stdlib.h"
#include "stdio.h"
#include "list.h"
// DOUBLE
int llist_double_init(llist_double *llist) {
llist->first = NULL;
llist->last = NULL;
llist->len = 0;
return 0;
}
void llist_double_destroy(llist_double *llist) {
llist_item_double *cur, *prev;
cur = llist->first;
while (cur != NULL) {
prev = cur;
cur = prev->next;
free(prev);
}
llist->len = 0;
}
void llist_double_print(llist_double *llist) {
llist_item_double *cur;
cur = llist->first;
while (cur != NULL) {
printf("%.2f ", cur->data);
cur = cur->next;
}
printf("\n");
}
int llist_double_push_back(llist_double *llist, double data) {
llist_item_double *new_item = (llist_item_double *) malloc(sizeof(llist_item_double));
if (new_item == NULL) {
return 1;
}
new_item->data = data;
new_item->next = NULL;
new_item->prev = llist->last;
if (llist->last == NULL) {
llist->last = new_item;
llist->first = new_item;
} else {
llist->last->next = new_item;
llist->last = new_item;
}
llist->len++;
return 0;
}
long int llist_double_size(llist_double *llist) {
return llist->len;
}
double llist_double_back(llist_double *llist) {
return llist->last->data;
}
double llist_double_front(llist_double *llist) {
return llist->first->data;
}
// LONG INT
int llist_li_init(llist_li *llist) {
llist->first = NULL;
llist->last = NULL;
llist->len = 0;
return 0;
}
void llist_li_destroy(llist_li *llist) {
llist_item_li *cur, *prev;
cur = llist->first;
while (cur != NULL) {
prev = cur;
cur = prev->next;
free(prev);
}
llist->len = 0;
}
void llist_li_print(llist_li *llist) {
llist_item_li *cur;
cur = llist->first;
while (cur != NULL) {
printf("%lu ", cur->data);
cur = cur->next;
}
printf("\n");
}
int llist_li_push_back(llist_li *llist, long int data) {
llist_item_li *new_item = (llist_item_li *) malloc(sizeof(llist_item_li));
if (new_item == NULL) {
return 1;
}
new_item->data = data;
new_item->next = NULL;
new_item->prev = llist->last;
if (llist->last == NULL) {
llist->last = new_item;
llist->first = new_item;
} else {
llist->last->next = new_item;
llist->last = new_item;
}
llist->len++;
return 0;
}
long int llist_li_size(llist_li *llist) {
return llist->len;
}
long int llist_li_back(llist_li *llist) {
return llist->last->data;
}
long int llist_li_front(llist_li *llist) {
return llist->first->data;
}
// remove item from its current list (position) and move to back of another list
int llist_li_relink(llist_item_li *item, llist_li *from, llist_li *to) {
llist_item_li *pred;
llist_item_li *succ;
if ((item == NULL) || (from->len == 0))
return 1;
pred = item->prev;
succ = item->next;
// append item to the end of 'to'
item->prev = to->last;
item->next = NULL;
if (to->last != NULL) {
to->last->next = item;
}
to->last = item;
if (to->first == NULL) {
to->first = item;
}
// relink pred and succ in 'from'
if (pred != NULL) {
pred->next = succ;
} else {
from->first = succ;
}
if (succ != NULL) {
succ->prev = pred;
} else {
from->last = pred;
}
from->len--;
to->len++;
return 0;
}
// append all items from a given list to the end of another list
int llist_li_relink_all(llist_li *from, llist_li *to) {
if (from->first == NULL) {
// source is empty
return 0;
}
if (to->last == NULL) {
// destination is empty
to->first = from->first;
to->last = from->last;
to->len = from->len;
} else {
from->first->prev = to->last;
to->last->next = from->first;
to->last = from->last;
to->len += from->len;
}
from->first = NULL;
from->last = NULL;
from->len = 0;
return 0;
}
// POINTER
int llist_ptr_init(llist_ptr *llist) {
llist->first = NULL;
llist->last = NULL;
llist->len = 0;
return 0;
}
void llist_ptr_destroy(llist_ptr *llist) {
llist_item_ptr *cur, *prev;
cur = llist->first;
while (cur != NULL) {
prev = cur;
cur = prev->next;
free(prev);
}
llist->len = 0;
}
void llist_ptr_print(llist_ptr *llist) {
llist_item_ptr *cur;
cur = llist->first;
while (cur != NULL) {
printf("%lx ", (long unsigned int) cur->data);
cur = cur->next;
}
printf("\n");
}
int llist_ptr_push_back(llist_ptr *llist, void *data) {
llist_item_ptr *new_item = (llist_item_ptr *) malloc(sizeof(llist_item_ptr));
if (new_item == NULL) {
return 1;
}
new_item->data = data;
new_item->next = NULL;
new_item->prev = llist->last;
if (llist->last == NULL) {
llist->last = new_item;
llist->first = new_item;
} else {
llist->last->next = new_item;
llist->last = new_item;
}
llist->len++;
return 0;
}
long int llist_ptr_size(llist_ptr *llist) {
return llist->len;
}
void *llist_ptr_back(llist_ptr *llist) {
return llist->last->data;
}
void *llist_ptr_front(llist_ptr *llist) {
return llist->first->data;
}