-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
97 lines (82 loc) · 1.18 KB
/
lists.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
93
94
95
96
97
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include "types.h"
void *alloc(size_t s)
{
void *p;
if (p = malloc(s)) {
memset(p, 0, s);
return p;
}
fprintf(stderr, "couldn't allocate memory\n");
exit(1);
}
List *push(List **l, void *tp)
{
List * t = (List*) tp;
if (!l)
return NULL;
if (*l)
(*l)->prev = t;
t->next = *l;
return *l = t;
}
List *pop(List **l)
{
List *t;
if (!l)
return NULL;
t = *l;
*l = (*l)->next;
if (*l)
(*l)->prev = NULL;
return t;
}
List *last(void *lp)
{
List *l = (List*) lp;
while (l && l->next)
l = l->next;
return l;
}
List *join(List **d, List **s)
{
if (*s) {
if (*d)
((*d)->prev = last(*s))->next = *d;
*d = *s;
*s = NULL;
}
return *d;
}
List *find(List **l, char *name)
{
List *t;
for (t = *l; t; t = t->next)
if (strcmp(t->name, name) == 0)
break;
return t;
}
List *take(List **l, void *tp)
{
List *t = (List*) tp;
if (!t)
return NULL;
if (t->prev)
t->prev->next = t->next;
else
*l = t->next;
if (t->next)
t->next->prev = t->prev;
t->next = t->prev = NULL;
return t;
}
void lfree(void *lp)
{
List *l = (List*) lp;
if (l->name)
free(l->name);
free(l);
}