AmendHub

Download

cyberslak

/

lightsout

/

list.c

 

(View History)

cyberslak   Add LICENSE information Latest amendment: 22 on 2025-03-15

1 // SPDX-License-Identifier: MIT
2
3 #include "list.h"
4
5 /* Unlink a node from a list. */
6 void list_del(list_t* node)
7 {
8 node->prev->next = node->next;
9 node->next->prev = node->prev;
10
11 node->next = node->prev = node;
12 }
13
14 /* Add an element after another in a list. */
15 void list_add(list_t* node, list_t* to_add)
16 {
17 to_add->next = node->next;
18 to_add->prev = node;
19
20 node->next->prev = to_add;
21 node->next = to_add;
22 }
23
24 void list_add_tail(list_t* node, list_t* to_add)
25 {
26 to_add->next = node;
27 to_add->prev = node->prev;
28
29 node->prev->next = to_add;
30 node->prev = to_add;
31 }
32
33 list_t* list_pop(list_t* head)
34 {
35 list_t* node;
36 if (list_empty(head))
37 return NULL;
38
39 node = head->next;
40 list_del(node);
41
42 return node;
43 }
44
45 list_t* list_pop_tail(list_t* head)
46 {
47 list_t* node;
48 if (list_empty(head))
49 return NULL;
50
51 node = head->prev;
52 list_del(node);
53
54 return node;
55 }
56
57
58 /* Move the root of a list to a different location. */
59 void list_move(list_t* dst, list_t* src)
60 {
61 dst->next = dst->prev = dst;
62
63 if (list_empty(src))
64 return;
65
66 *dst = *src;
67 src->next = src->prev = src;
68 dst->next->prev = dst;
69 dst->prev->next = dst;
70 }