-
Notifications
You must be signed in to change notification settings - Fork 32
/
list.h
71 lines (60 loc) · 1.65 KB
/
list.h
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
/* Copyright 2013 Bliksem Labs. See the LICENSE file at the top-level directory of this distribution and at https://github.com/bliksemlabs/rrrr/. */
/* list.h */
#include <stdint.h>
#include <stdlib.h>
typedef struct list_node {
struct list_node *next;
void *payload;
} ListNode;
typedef struct {
ListNode *head;
ListNode *tail;
uint32_t size;
} LinkedList;
inline void LinkedList_init (LinkedList *list) {
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
inline LinkedList *LinkedList_new () {
LinkedList *list = malloc (sizeof(LinkedList));
LinkedList_init (list);
return list;
}
inline void LinkedList_destroy (LinkedList **list) {
free (*list);
*list = NULL;
}
/* Add to head */
inline void LinkedList_push (LinkedList *list, void *payload) {
ListNode *node = malloc (sizeof(ListNode));
node->payload = payload;
node->next = list->head;
list->head = node;
if (list->tail == NULL) list->tail = node;
list->size += 1;
}
/* Add to tail */
inline void LinkedList_enqueue (LinkedList *list, void *payload) {
ListNode *node = malloc (sizeof(ListNode));
node->payload = payload;
node->next = NULL;
if (list->tail == NULL) {
list->head = node;
list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
list->size += 1;
}
/* Remove from head of list */
inline void *LinkedList_pop (LinkedList *list) {
if (list->head == NULL) return NULL;
void *payload = list->head->payload;
ListNode *old_head = list->head;
list->head = list->head->next;
free (old_head);
list->size -= 1;
return payload;
}