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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#include "list.h"
struct List* llist_new()
{
struct List *list=MALLOC( sizeof(struct List) );
list->first = NULL;
list->last = NULL;
list->count = 0;
return list;
}
struct ListNode* llist_newn( void *ptr )
{
struct ListNode *node = MALLOC( sizeof( struct ListNode ) );
node->next = NULL;
node->val = ptr;
return node;
}
int llist_length( struct List *list )
{
int len;
struct ListNode *node=list->first;
for ( len=0; node; node=node->next,len++);
return len;
}
int llist_index( struct List *list, void *ptr )
{
int index;
struct ListNode *node = list->first;
for ( index=0; node; node=node->next, index++)
if ( node->val == ptr )
return index;
return -1;
}
void llist_reverse( struct List *list )
{
ERROR("\n");
//abort(0);
}
void llist_append( struct List *list, void *ptr )
{
//abort(0);
}
void llist_appendn( struct List **list, void *ptr, struct ListNode *node )
{
//abort(0);
}
void* llist_pop( struct List *list )
{
struct ListNode *node = list->last;
struct ListNode *iter = list->first;
if ( node )
{
/*
if ( node->prev != NULL )
{
void *ptr = node->val;
//node = node->prev;
//FREE( node->next );
//node->next = NULL;
//list->last = node;
return ptr;
}
*/
while (iter->next != node)
{
iter = iter->next;
}
void *ptr = node->val;
FREE(iter->next);
iter->next = NULL;
list->last = iter;
return ptr;
}
return NULL;
}
void llist_push( struct List *list, void *ptr )
{
struct ListNode *node = MALLOC( sizeof(struct ListNode) );
node->val = ptr;
node->next = NULL;
if ( list->last )
{
list->last->next = node;
list->last = node;
}
else
{
list->first = node;
list->last = node;
}
}
void* llist_removen( struct List *l, struct ListNode *ln )
{
ListNode *ret=NULL;
ListNode *iter = l->first;
ListNode *prev_iter = NULL;
if ( iter == NULL )
goto result_ready;
while ( iter->next != NULL )
{
if ( iter == ln )
{
break;
}
prev_iter = iter;
iter = iter->next;
}
ret = iter;
iter = NULL; //dont use iter anymore pls
if ( ret != NULL )
{
//if first element of list
//the prev=NULL
if ( prev_iter == NULL )
{
//if only one element in da list?
if ( l->first == l->last )
{
l->first = NULL;
l->last = NULL;
} else
//if more then one element
{
l->first = l->first->next;
}
ret->next = NULL;
goto result_ready;
}
//if last element of the list
if ( ret == l->last )
{
prev_iter->next = NULL;
l->last = prev_iter;
goto result_ready;
}
//if in da middle
prev_iter->next = ((ListNode *)ret)->next;
((ListNode *)ret)->next = NULL;
}
result_ready:
return ret;
}
void llist_free( struct List *list )
{
struct ListNode *node = list->first, *prev;
while ( node != list->last )
{
prev = node;
node = node->next;
if (prev != NULL)
{
FREE( prev->val );
FREE( prev );
}
}
if ( node )
{
if ( node->val )
FREE( node->val );
FREE( node );
}
list->first = NULL;
list->last = NULL;
FREE( list );
list = NULL;
}
void llist_freen( struct ListNode *node )
{
ERROR("\n");
}
void llist_sortn( struct List *list, clb_llist_cmp clb_cmp)
{
int print_list( List *ln )
{
if ( ln == NULL ) return;
char *ptr=NULL;
ListNode *l = ln->first;
while ( l != NULL )
{
ptr = l->val;
printf("%s", ptr);
if ( l->next != NULL )
{
printf("(->%s) ",(char*)l->next->val );
}
//printf("\n");
l = l->next;
}
printf("\n");
return 1;
}
int print_node( ListNode *n )
{
if ( n == NULL ) return;
while ( n != NULL )
{
printf("%s ", (char*)n->val);
n = n->next;
}
printf("\n");
}
//creat new empty list and add all elements sorted
struct ListNode *sorted=NULL, *last=NULL, *get=NULL;
get = llist_removen( list, list->first );
sorted = get;
get = llist_removen( list, list->first );
while ( get != NULL )
{
//add to empty in sorted like place
struct ListNode *iter = sorted;
struct ListNode *prev_iter = malloc( sizeof(struct ListNode));
memset( prev_iter, 0, sizeof(struct ListNode));
prev_iter->next = iter;
while ( iter != NULL )
{
//there could be that no empty element in da list
if ( get == NULL ) break;
if ( clb_cmp( iter->val, get->val ) >= 0 )
{
//add to sorted list end if everything is ok
if ( iter->next == NULL )
{
iter->next = get;
break;
}
} else
{
prev_iter->next = get;
prev_iter = get;
get->next = iter;
if ( iter == sorted )
{
sorted = prev_iter;
}
iter = prev_iter;
break;
}
prev_iter = iter;
iter = iter->next;
}
get = llist_removen( list, list->first );
}
list->first = sorted;
}
int llist_save( struct List *l, clb_llist_save clb_save )
{
int ret=0, frc=0;
if ( l == NULL ) return ret;
if ( clb_save == NULL ) return ret;
/*
struct ListNode *iter = llist_first( l );
while ( iter != NULL )
{
clb_save( );
iter = iter->next;
}
*/
frc = clb_save( "/tmp/binlist.txt", l );
if ( frc != 0)
ret = -1;
return ret;
}
struct List* llist_load( clb_llist_load clb_load, const char *fname )
{
int ret = 0;
struct List *loaded = NULL;
ret = clb_load( (char *)fname, (void **)loaded );
return loaded;
}
|