-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientLinkList.c
65 lines (48 loc) · 1.32 KB
/
clientLinkList.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
#include "clientLinkList.h"
clientLinkList *cll_create() {
clientLinkList *list = (clientLinkList *)malloc(sizeof(clientLinkList));
list->head = cll_createItem(NULL);
list->tail = cll_createItem(NULL);
list->head->next = list->tail;
list->tail->prev = list->head;
return list;
}
clientLink *cll_createItem(struct client *c) {
clientLink *link = (clientLink *)malloc(sizeof(clientLink));
link->c = c;
link->next = NULL;
link->prev = NULL;
return link;
}
void cll_addFirst(clientLinkList *list, struct client *c) {
clientLink *newLink = cll_createItem(c);
if (list == NULL) list = cll_create();
newLink->next = list->head->next;
newLink->prev = list->head;
list->head->next->prev = newLink;
list->head->next = newLink;
}
void cll_addLast(clientLinkList *list, struct client *c) {
clientLink *newLink = cll_createItem(c);
if (list == NULL) list = cll_create();
newLink->next = list->tail;
newLink->prev = list->tail->prev;
list->tail->prev->next = newLink;
list->tail->prev = newLink;
}
void cll_remove(clientLink *link) {
if (link != NULL) {
link->prev->next = link->next;
link->next->prev = link->prev;
free(link);
}
}
void cll_clear(clientLink *head) {
if (head != NULL) {
cll_clear(head->next);
free(head);
}
}
int cll_isEmpty(clientLinkList *list) {
return (list->head->next == list->tail);
}