-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
86 lines (62 loc) · 1.03 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
/* filename: list.c */
#include "list.h"
element_t *list_create_element()
{
element_t *e;
e = malloc(sizeof(element_t));
if(0 == e)
{
perror("malloc");
return 0;
}
e->next = 0;
return e;
}
void list_delete_element(element_t *e)
{
free(e);
return;
}
void list_append_element(element_t *list, element_t *e)
{
element_t *cur = list;
while(0 != cur->next)
cur = cur->next;
cur->next = e;
return;
}
int list_remove_element(element_t *list, element_t *e)
{
element_t *cur = list;
while(cur->next != e || cur->next == 0)
cur = cur->next;
if(0 == cur->next)
return -1;
if(cur->next->next != 0)
cur->next = cur->next->next;
return 0;
}
element_t *list_pop(element_t *list)
{
element_t *cur = list,
*e = list;
if( (0 != cur->next) )
{
while( cur->next->next != 0 )
cur = cur->next;
e = cur->next;
}
cur->next = 0;
return e;
}
int list_length(element_t* list)
{
int length = 1;
element_t *cur = list;
while(cur->next != 0)
{
cur = cur->next;
length++;
}
return length;
}