-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
174 lines (127 loc) · 2.71 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
/*
* Author: Chris Wailes <[email protected]>
* Project: Parallel Linear Program Solver
* Date: 2012/01/24
* Description: A list implementation.
*/
// Standard Includes
#include <stdlib.h>
// Project Includes
#include "list.h"
// Functions
list_t* list_create(void) {
list_t* list;
list = list_new();
list_init(list);
return list;
}
void list_destroy(list_t* list) {
list_node_t* node;
while (list->head != NULL) {
node = list->head;
list->head = node->next;
free(node);
}
}
void list_free(list_t* list) {
list_destroy(list);
free(list);
}
void list_init(list_t* list) {
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
list_t* list_new(void) {
return (list_t*)malloc(sizeof(list_t));
}
void list_push(list_t* list, void* data) {
list_node_t* node;
node = malloc(sizeof(list_node_t));
node->data = data;
node->next = NULL;
if (list->size++ != 0) {
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
} else {
node->prev = NULL;
list->head = list->tail = node;
}
}
void* list_pop(list_t* list) {
void* data;
list_node_t* node;
if (list->size == 0) {
return NULL;
}
node = list->tail;
data = node->data;
if (--list->size == 0) {
list->head = list->tail = NULL;
} else {
node->prev->next = NULL;
list->tail = node->prev;
}
free(node);
return data;
}
void list_shift(list_t* list, void* data) {
list_node_t* node;
node = malloc(sizeof(list_node_t));
node->data = data;
node->prev = NULL;
if (list->size++ != 0) {
list->head->prev = node;
node->next = list->head;
list->head = node;
} else {
node->next = NULL;
list->head = list->tail = node;
}
}
void* list_unshift(list_t* list) {
void* data;
list_node_t* node;
if (list->size == 0) {
return NULL;
}
node = list->head;
data = node->data;
if (--list->size == 0) {
list->head = list->tail = NULL;
} else {
node->next->prev = NULL;
list->head = node->next;
}
free(node);
return data;
}
void list_insert_after(list_t* list, list_node_t* node, void* data) {
list_node_t* new_node;
if (node == list->tail) {
list_push(list, data);
} else {
new_node = malloc(sizeof(list_node_t));
new_node->data = data;
new_node->next = node->next;
new_node->prev = node;
node->next->prev = new_node;
node->next = new_node;
++list->size;
}
}
void list_insert_before(list_t* list, list_node_t* node, void* data) {
list_node_t* new_node;
if (node == list->head) {
list_shift(list, data);
} else {
new_node = malloc(sizeof(list_node_t));
new_node->data = data;
new_node->next = node;
new_node->prev = node->prev;
node->prev->next = new_node;
node->prev = new_node;
++list->size;
}
}