-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathclist.c
112 lines (78 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// clist.c
// Algorithms - circular list
//
// Created by YourtionGuo on 24/04/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdlib.h>
#include <string.h>
#include "clist.h"
#pragma mark - Public
void clist_init(CList *list, void (*destroy)(void *data))
{
/// 初始化链表
list->size = 0;
list->destroy = destroy;
list->head = NULL;
return;
}
void clist_destroy(CList *list)
{
void *data;
/// 删除每一个元素
while (clist_size(list) > 0) {
if (clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) {
/// 调用用户定义的 destroy 方法销毁动态创建的数据
list->destroy(data);
}
}
/// 清理链表结构体数据
memset(list, 0, sizeof(CList));
return;
}
int clist_ins_next(CList *list, CListElmt *element, const void *data)
{
CListElmt *new_element;
/// 分配元素需要的空间
if ((new_element = (CListElmt *)malloc(sizeof(CListElmt))) == NULL) return -1;
/// 插入元素到链表
new_element->data = (void *)data;
if (clist_size(list) == 0) {
/// 处理插入空循环链表
new_element->next = new_element;
list->head = new_element;
} else {
/// 处理非空情况
new_element->next = element->next;
element->next = new_element;
}
/// 更新链表的 size
list->size++;
return 0;
}
int clist_rem_next(CList *list, CListElmt *element, void **data)
{
CListElmt *old_element;
/// 禁止删除空链表的数据
if (clist_size(list) == 0) return -1;
/// 从链表中删除元素
*data = element->next->data;
if (element->next == element) {
/// 处理删除最后一个元素
old_element = element->next;
list->head = NULL;
} else {
/// 处理其他情况
old_element = element->next;
element->next = element->next->next;
if (old_element == clist_head(list)) {
list->head = old_element->next;
}
}
/// 清理旧结点元素的
free(old_element);
/// 更新链表的 size
list->size--;
return 0;
}