forked from zhemao/libds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
145 lines (125 loc) · 2.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
#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->data;
}
void* list_last(list_p list){
return list->last->data;
}
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);
}