-
Notifications
You must be signed in to change notification settings - Fork 37
/
list.c
206 lines (181 loc) · 4.2 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
#include "list.h"
#include <stdlib.h>
#include <string.h>
list_p create_list(){
list_p list = (list_p) malloc(sizeof(struct list));
list->length = 0;
list->first = NULL;
list->last = NULL;
list->destructor = free;
return list;
}
list_iter_p list_iterator(list_p list, char init){
list_iter_p iter = (list_iter_p)malloc(sizeof(struct list_iter));
if(init==FRONT){
iter->current = list->first;
}
else if(init==BACK){
iter->current = list->last;
}
else return NULL;
iter->started = 0;
return iter;
}
void list_add(list_p list, void* data, int size){
lnode_p node = (lnode_p)malloc(sizeof(struct linked_node));
node->data = malloc(size);
memcpy(node->data, data, size);
if(list->first==NULL){
node->prev = NULL;
node->next = NULL;
list->first = node;
list->last = node;
}
else{
list->last->next = node;
node->prev = list->last;
node->next = NULL;
list->last = node;
}
list->length++;
}
void* list_current(list_iter_p iter){
if(iter->started&&iter->current!=NULL)
return iter->current->data;
return NULL;
}
void* list_next(list_iter_p iter){
if(!iter->started&&iter->current!=NULL){
iter->started=1;
return iter->current->data;
}
if(iter->current!=NULL){
iter->current = iter->current->next;
return list_current(iter);
}
return NULL;
}
void* list_prev(list_iter_p iter){
if(!iter->started&&iter->current!=NULL){
iter->started=1;
return iter->current->data;
}
if(iter->current!=NULL){
iter->current = iter->current->prev;
return list_current(iter);
}
return NULL;
}
void* list_first(list_p list){
return list->first ? list->first->data : list->first;
}
void* list_last(list_p list){
return list->last ? list->last->data : list->last;
}
int list_empty(list_p list){
return list->first ? 0 : 1;
}
void* list_pop(list_p list){
lnode_p last = list->last;
if(last == NULL) return NULL;
if (list->first == list->last) {
list->first = NULL;
list->last = NULL;
} else {
list->last = last->prev;
last->prev->next = NULL;
}
void* data = last->data;
free(last);
list->length--;
return data;
}
void* list_poll(list_p list){
lnode_p first = list->first;
if(first == NULL)
return NULL;
if (list->first == list->last) {
list->first = NULL;
list->last = NULL;
} else {
list->first = first->next;
first->next->prev = NULL;
}
void* data = first->data;
free(first);
list->length--;
return data;
}
void list_remove(list_p list, char end){
void * data;
if(end == FRONT)
data = list_poll(list);
else if (end == BACK)
data = list_pop(list);
else return;
list->destructor(data);
}
void destroy_list(list_p list){
lnode_p cur = list->first;
lnode_p next;
while(cur!=NULL){
next = cur->next;
list->destructor(cur->data);
free(cur);
cur = next;
}
free(list);
}
void list_insert(list_p list, lnode_p before, void *data, int size){
if (list->first == NULL) {
list_add(list, data, size);
}
else if (before == list->last) {
list_add(list, data, size);
}
else if (before == NULL) {
lnode_p node = (lnode_p)malloc(sizeof(struct linked_node));
node->data = malloc(size);
memcpy(node->data, data, size);
node->next = list->first;
node->prev = NULL;
node->next->prev = node;
list->first = node;
list->length++;
}
else {
lnode_p node = (lnode_p)malloc(sizeof(struct linked_node));
node->data = malloc(size);
memcpy(node->data, data, size);
node->next = before->next;
node->prev = before;
before->next = node;
node->next->prev = node;
list->length++;
}
}
void* list_pluck(list_p list, lnode_p removed){
if(removed == NULL){
return NULL;
}
else if(list->first == removed && list->last == removed){
list->first = NULL;
list->last = NULL;
}
else if(list->first == removed){
list->first = removed->next;
removed->next->prev = NULL;
}
else if(list->last == removed){
list->last = removed->prev;
removed->prev->next = NULL;
}
else{
removed->prev->next = removed->next;
removed->next->prev = removed->prev;
}
void* data = removed->data;
free(removed);
list->length--;
return data;
}