~kennylevinsen/seatd

ref: 3ad9164a896eacffd27588f5b1fd6ea3e0bcd92d seatd/common/linked_list.c -rw-r--r-- 1.1 KiB
3ad9164aKenny Levinsen Bump version to 0.6.0 4 months ago
                                                                                
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
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

#include "linked_list.h"

void linked_list_init(struct linked_list *list) {
	list->next = list;
	list->prev = list;
}

void linked_list_insert(struct linked_list *list, struct linked_list *elem) {
	assert(list->prev != NULL && list->next != NULL);
	assert(elem->prev == NULL && elem->next == NULL);

	elem->prev = list;
	elem->next = list->next;
	list->next = elem;
	elem->next->prev = elem;
}

void linked_list_remove(struct linked_list *elem) {
	assert(elem->prev != NULL && elem->next != NULL);

	elem->prev->next = elem->next;
	elem->next->prev = elem->prev;
	elem->next = NULL;
	elem->prev = NULL;
}

bool linked_list_empty(struct linked_list *list) {
	assert(list->prev != NULL && list->next != NULL);
	return list->next == list;
}

void linked_list_take(struct linked_list *target, struct linked_list *source) {
	if (linked_list_empty(source)) {
		return;
	}

	source->next->prev = target;
	source->prev->next = target->next;
	target->next->prev = source->prev;
	target->next = source->next;
	linked_list_init(source);
}