-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclist.c
93 lines (75 loc) · 2.08 KB
/
clist.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
/* clist.c */
#include <stdlib.h>
#include <string.h>
#include "clist.h"
/* clist_init */
void clist_init(CList *list, void (*destroy)(void *data)){
/* Initialize the list. */
list->size = 0;
list->destroy = destroy;
list->head = NULL;
return;
}
/* clist_destroy */
void clist_destroy(CList *list){
void *data;
/* Remove each element. */
while (clist_size(list) > 0) {
if (clist_rem_next(list, list->head, (void **)data) == 0
&& list->destroy != NULL) {
list->destroy(data);
}
}
/* No operations are allowed now, but clear the structure as a precaution. */
memset(list, 0, sizeof(CList));
return;
}
/* clist_ins_next */
int clist_ins_next(CList *list, CListElmt *element, const void *data){
CListElmt *new_element;
/* Allocate storage for the element. */
if ((new_element = (CListElmt *)malloc(sizeof(CListElmt))) == NULL) {
return -1;
}
/* Insert the element into the list. */
new_element->data = (void *)data;
if (clist_size(list) == 0) {
/* Handle insertion when the list is empty. */
new_element->next = new_element;
list->head = new_element;
} else {
/* Handle insertion when the list is not empty. */
new_element->next = element->next;
element->next = new_element;
}
/* Adjust the size of list to account for the inserted element. */
list->size++;
return 0;
}
/* clist_rem_next */
int clist_rem_next(CList *list, CListElmt *element, void **data){
CListElmt *old_element;
/* Don't allow removal from an empty list. */
if (clist_size(list) == 0) {
return -1;
}
/* Remove the element from the list. */
*data = element->next->data;
if (element->next == element) {
/* Handle removing the last element. */
old_element = element->next;
list->head = NULL;
} else {
/* Handle removing other than the last element. */
old_element = element->next;
element->next = element->next->next;
if (old_element == clist_head(list)) {
list->head = old_element->next;
}
}
/* Free the storage allocated by thr abstract datatype. */
free(old_element);
/* Adjust the size of the list to account for the removed element. */
list->size--;
return 0;
}