-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
54 lines (44 loc) · 952 Bytes
/
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
#include <stdlib.h>
#include "list.h"
void
list_init(struct list * lst)
{
struct link * first = (struct link *) malloc(sizeof(struct link));
struct link * last = (struct link *) malloc(sizeof(struct link));
lst->first = first;
first->next = last;
last->prev = first;
lst->last = last;
}
void
list_free(struct list * l)
{
free(l->last);
free(l->first);
free(l);
}
void
list_push(struct list * l, void * e)
{
struct link * lnk = (struct link *) malloc(sizeof(struct link));
lnk->elem = e;
l->first->next->prev = lnk;
lnk->next = l->first->next;
lnk->prev = l->first;
l->first->next = lnk;
}
void *
list_pop(struct list * l)
{
void * e = l->last->prev->elem;
struct link * lst = l->last->prev;
l->last->prev->prev->next = l->last;
l->last->prev = lst->prev;
free(lst);
return e;
}
int
list_empty(struct list * l)
{
return (l->first->next == l->last);
}