-
Notifications
You must be signed in to change notification settings - Fork 274
/
dlist.c
174 lines (122 loc) · 3.45 KB
/
dlist.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// dlist.c
// Algorithms - doubly-linked list
//
// Created by YourtionGuo on 24/04/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include "dlist.h"
#pragma mark - Public
void dlist_init(DList *list, void (*destory)(void *data))
{
/// 初始化链表
list->size = 0;
list->destroy = destory;
list->head = NULL;
list->tail = NULL;
return;
}
void dlist_destroy(DList *list)
{
void *data;
/// 删除每一个元素
while (dlist_size(list) > 0) {
if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL) {
/// 调用用户定义的 destroy 方法销毁动态创建的数据
list->destroy(data);
}
}
/// 清理链表结构体数据
memset(list, 0, sizeof(DList));
return;
}
int dlist_ins_next(DList *list, DListElmt *element, const void *data)
{
DListElmt *new_element;
/// 除非是空双向链表,否则不允许 element 为 NULL
if (element == NULL && dlist_size(list) != 0) return -1;
/// 分配元素需要的空间
if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1;
/// 插入元素到链表
new_element->data = (void *)data;
if (dlist_size(list) == 0) {
/// 处理空双向链表
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
} else {
/// 处理非空双向链表
new_element->next = element->next;
new_element->prev = element;
if (element->next == NULL) {
list->tail = new_element;
} else {
element->next->prev = new_element;
}
element->next = new_element;
}
/// 更新链表的 size
list->size++;
return 0;
}
int dlist_ins_prev(DList *list, DListElmt *element, const void *data)
{
DListElmt *new_element;
/// 除非是空双向链表,否则不允许 element 为 NULL
if (element == NULL && dlist_size(list) != 0) return -1;
/// 分配元素需要的空间
if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1;
/// 插入元素到链表
new_element->data = (void *)data;
if (dlist_size(list) == 0) {
/// 处理空双向链表
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
} else {
/// 处理非空双向链表
new_element->next = element;
new_element->prev = element->prev;
if (element->prev == NULL) {
list->head = new_element;
} else {
element->prev->next = new_element;
}
element->prev = new_element;
}
/// 更新链表的 size
list->size++;
return 0;
}
int dlist_remove(DList *list, DListElmt *element, void **data)
{
/// 禁止删除 element 为空或空链表的数据
if (element == NULL || dlist_size(list) == 0) return -1;
*data = element->data;
if (element == list->head) {
/// 处理删除头部元素
list->head = element->next;
if (list->head == NULL) {
list->tail = NULL;
} else {
element->next->prev = NULL;
}
} else {
/// 处理删除非头部元素
element->prev->next = element->next;
if (element->next == NULL) {
list->tail = element->prev;
} else {
element->next->prev = element->prev;
}
}
/// 清理旧结点元素的
free(element);
/// 更新链表的 size
list->size--;
return 0;
}