darknet
v3
src
list.c
Go to the documentation of this file.
1
#include <stdlib.h>
2
#include <string.h>
3
#include "
list.h
"
4
5
list
*
make_list
()
6
{
7
list
*l = malloc(
sizeof
(
list
));
8
l->
size
= 0;
9
l->
front
= 0;
10
l->
back
= 0;
11
return
l;
12
}
13
14
/*
15
void transfer_node(list *s, list *d, node *n)
16
{
17
node *prev, *next;
18
prev = n->prev;
19
next = n->next;
20
if(prev) prev->next = next;
21
if(next) next->prev = prev;
22
--s->size;
23
if(s->front == n) s->front = next;
24
if(s->back == n) s->back = prev;
25
}
26
*/
27
28
void
*
list_pop
(
list
*l){
29
if
(!l->
back
)
return
0;
30
node
*b = l->
back
;
31
void
*val = b->
val
;
32
l->
back
= b->
prev
;
33
if
(l->
back
) l->
back
->
next
= 0;
34
free(b);
35
--l->
size
;
36
37
return
val;
38
}
39
40
void
list_insert
(
list
*l,
void
*val)
41
{
42
node
*
new
= malloc(
sizeof
(
node
));
43
new
->val = val;
44
new
->next = 0;
45
46
if
(!l->
back
){
47
l->
front
=
new
;
48
new
->
prev
= 0;
49
}
else
{
50
l->
back
->
next
=
new
;
51
new
->
prev
= l->
back
;
52
}
53
l->
back
=
new
;
54
++l->
size
;
55
}
56
57
void
free_node
(
node
*n)
58
{
59
node
*next;
60
while
(n) {
61
next = n->
next
;
62
free(n);
63
n = next;
64
}
65
}
66
67
void
free_list
(
list
*l)
68
{
69
free_node
(l->
front
);
70
free(l);
71
}
72
73
void
free_list_contents
(
list
*l)
74
{
75
node
*n = l->
front
;
76
while
(n){
77
free(n->
val
);
78
n = n->
next
;
79
}
80
}
81
82
void
**
list_to_array
(
list
*l)
83
{
84
void
**a = calloc(l->
size
,
sizeof
(
void
*));
85
int
count = 0;
86
node
*n = l->
front
;
87
while
(n){
88
a[count++] = n->
val
;
89
n = n->
next
;
90
}
91
return
a;
92
}
list_insert
void list_insert(list *l, void *val)
Definition:
list.c:40
node
Definition:
darknet.h:596
free_node
void free_node(node *n)
Definition:
list.c:57
list_to_array
void ** list_to_array(list *l)
Definition:
list.c:82
node::next
struct node * next
Definition:
darknet.h:598
list_pop
void * list_pop(list *l)
Definition:
list.c:28
list::size
int size
Definition:
darknet.h:603
list::back
node * back
Definition:
darknet.h:605
list.h
free_list
void free_list(list *l)
Definition:
list.c:67
node::val
void * val
Definition:
darknet.h:597
free_list_contents
void free_list_contents(list *l)
Definition:
list.c:73
list::front
node * front
Definition:
darknet.h:604
node::prev
struct node * prev
Definition:
darknet.h:599
list
Definition:
darknet.h:602
make_list
list * make_list()
Definition:
list.c:5
Generated by
1.8.13