-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
225 lines (179 loc) · 5.28 KB
/
list.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
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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
SList* ListCreate()
{
SList* list = (SList*) malloc(sizeof(SList));
list->first = NULL;
list->last = NULL;
list->size = 0;
return list;
}
void ListDestroy (SList** list)
{
if (list == NULL || *list == NULL)
return;
ListClear (*list);
free (*list);
*list = NULL;
}
void ListAddElement (SList* list, void* value)
{
if (list == NULL)
return;
SListElement* newElement = (SListElement*) malloc(sizeof(SListElement));
newElement->value = value;
newElement->next = NULL;
list->size++;
// if it is first element
if (list->first == NULL)
{
newElement->prev = NULL;
list->first = newElement;
list->last = newElement;
//printf ("add as first\n");
}
else
{
newElement->prev = list->last;
list->last->next = newElement;
list->last = newElement;
//printf ("add to end\n");
}
}
void ListDeleteElementByValue (SList* list, void* value)
{
if (list == NULL || value == NULL)
return;
if (list->first == NULL)
return;
// ïðîáåãàåìñÿ ïî âñåì ýëåìåíòàì, ïîêà íå íàòêíåìñÿ íà çíà÷åíèå èëè íà êîíåö...
SListElement* element = list->first;
while ((element->value != value) && (element != NULL))
element = element->next;
// åñëè ÷òî-òî íàø¸ë, òî óäàëÿåì ýëåìåíò è ñöåïëÿåì ñîñåäåé
if (element != NULL && element->value == value)
{
//printf ("try to delete value...");
if (element->value)
{
free (element->value);
element->value = NULL;
}
//printf ("\n\t\tdone\n");
list->size --;
// ýëåìåíò ãäå-òî ìåæäó ãîëîâîé è õâîñòîì
if (element->prev != NULL && element->next != NULL)
{
//printf ("deleting anything...");
element->prev->next = element->next;
element->next->prev = element->prev;
//printf ("\n\t\tdone\n");
goto element_delete;
}
// ýëåìåíò ãîëîâíîé
if (element->prev == NULL && element->next != NULL)
{
//printf ("deleting head...");
list->first = element->next;
element->next->prev = NULL;
//printf ("\n\t\tdone\n");
goto element_delete;
}
// ýëåìåíò õâîñòîâîé
if (element->prev != NULL && element->next == NULL)
{
//printf ("deleting tail...");
list->last = element->prev;
element->prev->next = NULL;
//printf ("\n\t\tdone\n");
goto element_delete;
}
// ýëåìåíò ïîñëåäíèé
if ((list->size <= 0) || (element->prev == NULL && element->next == NULL))
{
//printf ("deleting last...");
// óæå âñ¸ óäàëåíî. Ãóäáàé
list->first = NULL;
list->last = NULL;
//element = NULL;
list->size = 0;
//printf ("all deleted! list is empty now!\n");
}
element_delete:
//printf ("try to delete element...");
if (element)
{
free (element);
element = NULL;
}
//printf ("\n\t\tdone\n");
}
}
void ListDeleteElementByNymber (SList* list, uint32 numOfElement)
{
if (list == NULL || (numOfElement > list->size))
return;
uint32 num = 0;
SListElement* element = list->first;
while ((num != numOfElement) && (element != NULL))
element = element->next;
if (element != NULL && element->value != NULL)
ListDeleteElementByValue (list, element->value);
}
void* ListGetElement (SList* list, uint32 numOfElement)
{
if (list == NULL || (numOfElement > list->size))
return NULL;
/* search element for return value */
SListElement* element = list->first;
for (uint32 i = 0; i < numOfElement; i++)
{
element = element->next;
}
if (element == NULL)
return NULL;
else
return element->value;
}
uint32 ListGetSize (SList* list)
{
if (list == NULL)
return 0;
return list->size;
}
void ListClear (SList* list)
{
if (list == NULL || list->first == NULL)
return;
/* ïðîõîäèìñÿ îò ïåðâîãî äî ïîñëåäíåãî */
SListElement* element = list->first;
SListElement* nextElement;
while (element != NULL)
{
nextElement = element->next;
/* óäàëÿåì çíà÷åíèå, åñëè òàêîâîå èìååòñÿ */
if (element->value)
{
//printf("\n\t\ttry to delete value...");
free (element->value);
element->value = NULL;
//printf ("done\n");
}
/* óäàëÿåì ýëåìåíò, åñëè åñòü */
if (element)
{
//printf("\n\t\ttry to delete element...");
free (element);
element = NULL;
//printf ("done\n");
}
element = nextElement;
}
/* ÷èñòèì óêàçàòåëè íà ïåðâûé è ïîñëåäíèé ýëåìåíòû */
if (list->first)
list->first = NULL;
if (list->last)
list->last = NULL;
list->size = 0;
}