forked from ranbenbasat/RHHH
-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_list.c
144 lines (118 loc) · 2.81 KB
/
linked_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 "linked_list.h"
linked_list_t *ll_create() {
linked_list_t *list = calloc(sizeof(linked_list_t), 1);
list->head = NULL;
list->tail = NULL;
list->length = 0;
return list;
}
void ll_destroy(linked_list_t *list) {
if (list == NULL)
return;
ll_node_t *curr = list->head;
ll_node_t *next = NULL;
while (curr != NULL) {
next = curr->next;
free(curr);
curr = next;
}
free(list);
}
ll_node_t *ll_create_node(void *object) {
ll_node_t *node = calloc(sizeof(ll_node_t), 1);
node->next = NULL;
node->prev = NULL;
node->object = object;
return node;
}
ll_node_t *ll_add(linked_list_t *list, void *object) {
if (list == NULL || object == NULL)
return NULL;
ll_node_t *node = ll_create_node(object);
/* List is empty. */
if (list->head == NULL) {
list->head = node;
list->tail = node;
}
/* List has one or more elements. */
else {
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
}
list->length++;
return node;
}
ll_node_t *ll_add_front(linked_list_t *list, void *object) {
if (list == NULL || object == NULL)
return NULL;
ll_node_t *node = ll_create_node(object);
/* List is empty. */
if (list->head == NULL) {
list->head = node;
list->tail = node;
}
/* List has one or more elements. */
else {
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->length++;
return node;
}
ll_node_t *ll_add_after(linked_list_t *list, ll_node_t *node, void *object) {
if (list == NULL || node == NULL || object == NULL)
return NULL;
ll_node_t *new_node = ll_create_node(object);
/* Update pointers. */
new_node->prev = node;
new_node->next = node->next;
if (node->next != NULL)
node->next->prev = new_node;
node->next = new_node;
/* Added to end of list. */
if (node == list->tail)
list->tail = new_node;
list->length++;
return new_node;
}
void *ll_remove(linked_list_t *list, ll_node_t *node) {
if (list == NULL || node == NULL)
return NULL;
void *object = node->object;
/* Update linked list pointers. */
if (node == list->head)
list->head = node->next;
else
node->prev->next = node->next;
if (node == list->tail)
list->tail = node->prev;
else
node->next->prev = node->prev;
/* Free memory. */
free(node);
list->length--;
return object;
}
ll_node_t *ll_find(linked_list_t *list, void *object) {
if (list == NULL || object == NULL)
return NULL;
ll_node_t *curr = list->head;
while (curr != NULL) {
if (curr->object == object) {
return curr;
}
curr = curr->next;
}
return NULL;
}
ll_node_t *ll_front(linked_list_t *list) {
return list->head;
}
ll_node_t *ll_back(linked_list_t *list) {
return list->tail;
}
unsigned int ll_length(linked_list_t *list) {
return list->length;
}