-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_two_way_list.h
244 lines (225 loc) · 7.68 KB
/
linked_two_way_list.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdlib.h>
// Элемент двусвязного списка
struct TwoWayNode {
double number;
struct TwoWayNode* prev;
struct TwoWayNode* next;
};
// typedef позволяет не набирать [struct] TwoWayNode
// Элемент двусвязного списка
typedef struct TwoWayNode TwoWayNode;
// Создать новый элемент двусвязного списка
TwoWayNode* LinkedTwoWayList_newEmpty() {
TwoWayNode* node = (TwoWayNode*)malloc(sizeof(TwoWayNode));
node->number = 0;
node->prev = NULL;
node->next = NULL;
return node;
}
// Создать новый элемент двусвязного списка со значением
TwoWayNode* LinkedTwoWayList_new(double number) {
TwoWayNode* node = (TwoWayNode*)malloc(sizeof(TwoWayNode));
node->number = number;
node->prev = NULL;
node->next = NULL;
return node;
}
// Создать новый элемент двусвязного списка с указателями
TwoWayNode* LinkedTwoWayList_newNode(double number, TwoWayNode* previous,
TwoWayNode* next) {
TwoWayNode* node = (TwoWayNode*)malloc(sizeof(TwoWayNode));
node->number = number;
node->prev = previous;
node->next = next;
return node;
}
// Найти начало двусвязного списка
TwoWayNode* LinkedTwoWayList_start(TwoWayNode* node) {
if (node == NULL)
return NULL;
while (node->prev != NULL)
node = node->prev;
return node;
}
// Найти конец двусвязного списка
TwoWayNode* LinkedTwoWayList_end(TwoWayNode* node) {
if (node == NULL)
return NULL;
while (node->next != NULL)
node = node->next;
return node;
}
// Вставить элемент после указанного
TwoWayNode* LinkedTwoWayList_append(TwoWayNode* prev, TwoWayNode* node) {
if (node == NULL)
return NULL;
if (prev == NULL)
return node;
TwoWayNode* next = prev->next;
if (next != NULL)
next->prev = node;
node->next = next;
prev->next = node;
node->prev = prev;
return node;
}
// Вставить символ перед указанным
// Возвращает новый элемент
TwoWayNode* LinkedTwoWayList_appendNumber(TwoWayNode* prev, double number) {
return LinkedTwoWayList_append(prev, LinkedTwoWayList_new(number));
}
// Вставить элемент перед указанным
// Возвращает новый элемент
TwoWayNode* LinkedTwoWayList_insert(TwoWayNode* next, TwoWayNode* node) {
if (node == NULL)
return NULL;
if (next == NULL)
return node;
TwoWayNode* prev = next->prev;
if (prev != NULL)
prev->next = node;
node->prev = prev;
next->prev = node;
node->next = next;
return node;
}
// Вставить символ перед указанным
// Возвращает новый элемент
TwoWayNode* LinkedTwoWayList_insertNumber(TwoWayNode* next, double number) {
return LinkedTwoWayList_insert(next, LinkedTwoWayList_new(number));
}
// Удалить следующий элемент
// Возвращает новый следующий элемент
TwoWayNode* LinkedTwoWayList_removeNext(TwoWayNode* previous) {
if (previous == NULL || previous->next == NULL)
return NULL;
TwoWayNode* next = previous->next->next;
free(previous->next);
previous->next = next;
if (next != NULL)
next->prev = previous;
return next;
}
// Удалить предыдущий элемент
// Возвращает новый предыдущий элемент
TwoWayNode* LinkedTwoWayList_removePrevious(TwoWayNode* next) {
if (next == NULL || next->prev == NULL)
return NULL;
TwoWayNode* previous = next->prev->prev;
free(next->prev);
if (previous != NULL)
previous->next = next;
next->prev = previous;
return previous;
}
// Удалить данный элемент
// Возвращает следующий элемент
TwoWayNode* LinkedTwoWayList_remove(TwoWayNode* node) {
TwoWayNode* next = NULL;
if (node == NULL)
return NULL;
if (node->prev != NULL)
node->prev->next = node->next;
if (node->next != NULL) {
next = node->next;
node->next->prev = node->prev;
}
free(node);
return next;
}
// Элемент с указанным индексом, считая от старта с нуля
TwoWayNode* LinkedTwoWayList_at(TwoWayNode* node, size_t index) {
TwoWayNode* n = node;
for (size_t i = 0; i < index; i++) {
if (n->next == NULL)
return NULL;
n = n->next;
}
return n;
}
// Отчистить данный элемент и удалить все остальные
TwoWayNode* LinkedTwoWayList_empty(TwoWayNode* node) {
TwoWayNode* next;
if (node->next != NULL) {
for (TwoWayNode* n = node->next; next != NULL; n = next) {
next = n->next;
free(n);
}
}
if (node->prev != NULL) {
for (TwoWayNode* n = node->prev; next != NULL; n = next) {
next = n->prev;
free(n);
}
}
node->next = NULL;
node->number = 0;
node->prev = NULL;
return node;
}
// Удалить все элементы массива
void LinkedTwoWayList_free(TwoWayNode* node) {
TwoWayNode* next;
if (node->next != NULL) {
for (TwoWayNode* n = node->next; next != NULL; n = next) {
next = n->next;
free(n);
}
}
if (node->prev != NULL) {
for (TwoWayNode* n = node->prev; next != NULL; n = next) {
next = n->prev;
free(n);
}
}
free(node);
}
// Скопировать массив; возвращает начало нового массива
TwoWayNode* LinkedTwoWayList_copy(TwoWayNode* node) {
TwoWayNode* new_node = NULL;
for (node = LinkedTwoWayList_start(node); node != NULL; node = node->next)
new_node = LinkedTwoWayList_appendNumber(new_node, node->number);
return new_node;
}
// Объединить множество массивов; первым аргументом должно быть передано
// количество массивов; исходные массиве не изменяются
TwoWayNode* LinkedTwoWayList_mergeCopy(size_t count, TwoWayNode* nodes[count]) {
TwoWayNode* new_node = NULL;
for (size_t i = 0; i < count; i++) {
for (TwoWayNode* node = LinkedTwoWayList_start(nodes[i]); node != NULL;
node = node->next) {
new_node = LinkedTwoWayList_appendNumber(new_node, node->number);
}
}
return new_node;
}
// Количество элементов списка
size_t LinkedTwoWayList_length(TwoWayNode* node) {
size_t len = 0;
for (TwoWayNode* end = LinkedTwoWayList_start(node); end != NULL;
end = end->next)
len++;
return len;
}
// Расстояние между элементами списка
// Если A == B, возвращает 0
// Если A справа от B или в другом списке, возвращает -1
// Если A == NULL или B == NULL, возвращает -1
size_t LinkedTwoWayList_distance(TwoWayNode* A, TwoWayNode* B) {
if (A == NULL || B == NULL)
return -1;
size_t len = 0;
int found = 0;
for (TwoWayNode* node = A; node != NULL; node = node->next) {
len++;
if (node->next == B) {
found = 1;
break;
}
}
return found ? len : -1;
}
// double* LinkedTwoWayList_asArray(TwoWayNode* node) {
// size_t length = LinkedTwoWayList_length(node);
// double array[length];
// }