-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
66 lines (50 loc) · 1.17 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
#include <stdlib.h>
#include <string.h>
#include "list.h"
/* generic list handling */
struct list *list_add(struct list *head, char *name) {
struct list *l;
l = (struct list *) malloc(sizeof(struct list));
l->len = strlen(name) + 1;
l->name = (char *) malloc(l->len);
memcpy(l->name, name, l->len);
l->next = head;
return l;
}
struct list *list_mult_add(struct list *head, struct list *new) {
struct list *p;
/* append the new list at the end of the current one */
if (head == NULL) {
head = new;
} else {
for (p = head; p->next != NULL; p = p->next)
;
p->next = new;
}
return head;
}
struct list *list_remove(struct list *head, char *name) {
struct list *p, *prev;
prev = NULL;
for (p = head; p != NULL; p = p->next) {
if (strncmp(name, p->name, p->len) == 0) {
if (prev == NULL)
head = p->next;
else
prev->next = p->next;
free(p->name);
free(p);
return head;
}
prev = p;
}
/* item not in list, we return the original one */
return head;
}
struct list *list_lookup(struct list *head, char *name) {
struct list *p;
for (p = head; p != NULL; p = p->next)
if (strncmp(name, p->name, p->len) == 0)
return p;
return NULL;
}