forked from ieatlint/mslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllist.c
92 lines (68 loc) · 1.38 KB
/
llist.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
#include "llist.h"
LListH *llist_init() {
LListH *list;
list = ( LListH * ) malloc( sizeof( LListH ) );
if( list == NULL )
return NULL;
list->first = NULL;
list->last = NULL;
list->len = 0;
return list;
}
void llist_append( LListH *list, int idx, short amp ) {
LList *node;
node = ( LList * ) malloc( sizeof( LList ) );
if( node == NULL )
return;
node->idx = idx;
node->amp = amp;
node->next = NULL;
if( list->first == NULL )
list->first = node;
if( list->last != NULL )
list->last->next = node;
list->last = node;
list->len++;
}
void llist_remove_idx( LListH *list, int idx ) {
LList *trav,*prev;
for( trav = list->first, prev = NULL; trav != NULL; trav = trav->next ) {
if( trav->idx == idx )
break;
prev = trav;
}
if( trav != NULL ) {/* if found */
if( prev != NULL ) {
prev->next = trav->next;
} else {
list->first = trav->next;
}
if( trav == list->last )
list->last = prev;
free( trav );
list->len--;
}
}
LListH *llist_free( LListH *list ) {
if( list == NULL )
return NULL;
llist_reinit( list );
free( list );
return NULL;
}
void llist_reinit( LListH *list ) {
LList *trav,*prev;
for( trav = list->first, prev = NULL; trav != NULL; ) {
prev = trav;
trav = trav->next;
if( prev ) {
free( prev );
prev = NULL;
}
}
if( prev )
free( prev );
list->first = NULL;
list->last = NULL;
list->len = 0;
}