-
Notifications
You must be signed in to change notification settings - Fork 8
/
list.c
130 lines (112 loc) · 2.58 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
#include <stdlib.h>
#include "list.h"
#include "util.h"
List *make_list(void) {
List *r = malloc(sizeof(List));
r->len = 0;
r->head = r->tail = NULL;
return r;
}
List *make_list1(void *e) {
List *r = make_list();
list_push(r, e);
return r;
}
void *make_node(void *elem) {
ListNode *r = malloc(sizeof(ListNode));
r->elem = elem;
r->next = NULL;
r->prev = NULL;
return r;
}
List *list_copy(List *list) {
List *r = make_list();
for (Iter *i = list_iter(list); !iter_end(i);)
list_push(r, iter_next(i));
return r;
}
void list_push(List *list, void *elem) {
ListNode *node = make_node(elem);
if (!list->head) {
list->head = node;
} else {
list->tail->next = node;
node->prev = list->tail;
}
list->tail = node;
list->len++;
}
void list_append(List *a, List *b) {
for (Iter *i = list_iter(b); !iter_end(i);)
list_push(a, iter_next(i));
}
void *list_pop(List *list) {
if (!list->head) return NULL;
void *r = list->tail->elem;
list->tail = list->tail->prev;
if (list->tail)
list->tail->next = NULL;
else
list->head = NULL;
list->len--;
return r;
}
void *list_shift(List *list) {
if (!list->head) return NULL;
void *r = list->head->elem;
list->head = list->head->next;
if (list->head)
list->head->prev = NULL;
else
list->tail = NULL;
list->len--;
return r;
}
void list_unshift(List *list, void *elem) {
ListNode *node = make_node(elem);
node->next = list->head;
if (list->head)
list->head->prev = node;
list->head = node;
if (!list->tail)
list->tail = node;
list->len++;
}
void *list_get(List *list, int index) {
if (index < 0 || list->len <= index)
return NULL;
ListNode *p = list->head;
for (int i = 0; i < index; i++)
p = p->next;
return p->elem;
}
void *list_head(List *list) {
return list->head ? list->head->elem : NULL;
}
void *list_tail(List *list) {
return list->tail ? list->tail->elem : NULL;
}
List *list_reverse(List *list) {
List *r = make_list();
for (Iter *i = list_iter(list); !iter_end(i);)
list_unshift(r, iter_next(i));
return r;
}
int list_len(List *list) {
return list->len;
}
Iter *list_iter(List *list) {
Iter *r = malloc(sizeof(Iter));
r->ptr = list->head;
return r;
}
void *iter_next(Iter *iter) {
if (!iter->ptr)
return NULL;
void *r = iter->ptr->elem;
iter->ptr = iter->ptr->next;
return r;
}
bool iter_end(Iter *iter) {
return !iter->ptr;
}