forked from WojciechMula/pyahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.c
114 lines (84 loc) · 1.71 KB
/
slist.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
/*
This is part of pyahocorasick Python module.
Linked list implementation.
Const time of:
* append
* prepend
* pop first
* get first/last
Author : Wojciech Muła, wojciech_mula@poczta.onet.pl
WWW : http://0x80.pl
License : public domain
*/
#include "slist.h"
ListItem*
list_item_new(const size_t size) {
ListItem* item = (ListItem*)memory_alloc(size);
if (item) {
item->__next = 0;
}
return item;
}
void
list_item_delete(ListItem* item) {
memory_free(item);
}
void
list_init(List* list) {
if (list) {
list->head = 0;
list->last = 0;
}
}
int
list_delete(List* list) {
ListItem* item;
ListItem* tmp;
ASSERT(list);
item = list->head;
while (item) {
tmp = item;
item = item->__next;
memory_free(tmp);
}
list->head = list->last = NULL;
return 0;
}
ListItem*
list_append(List* list, ListItem* item) {
ASSERT(list);
if (item) {
if (list->last) {
list->last->__next = item; // append
list->last = item; // set as last node
}
else
list->head = list->last = item;
}
return item;
}
ListItem*
list_push_front(List* list, ListItem* item) {
ASSERT(list);
if (list->head) {
item->__next = list->head;
list->head = item;
}
else
list->head = list->last = item;
return item;
}
ListItem*
list_pop_first(List* list) {
ListItem* item;
ASSERT(list);
if (list->head) {
item = list->head;
list->head = item->__next;
if (!list->head)
list->last = 0;
return item;
}
else
return NULL;
}