forked from paranlee/ludtm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
58 lines (46 loc) · 1.06 KB
/
Copy pathlist.c
File metadata and controls
58 lines (46 loc) · 1.06 KB
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
/**
*
* Yunseong Kim <ysk@kzalloc.com>
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <list.h>
struct a_list {
struct list_head list;
int val;
const char* str;
};
static void append(struct a_list* ptr, const char* str, int val)
{
struct a_list* tmp;
tmp = (struct a_list*)malloc(sizeof(struct a_list));
if (!tmp) {
perror("malloc");
exit(1);
}
tmp->str = str;
tmp->val = val;
list_add_tail(&(tmp->list), &(ptr->list));
}
int main()
{
struct a_list blist;
struct a_list* iter;
INIT_LIST_HEAD(&blist.list);
/* add item to list */
append(&blist, "NM", 87501);
append(&blist, "CA", 94041);
append(&blist, "IL", 60561);
/* iterates list */
list_for_each_entry(iter, &blist.list, list) {
printf("%s %d\n", iter->str, iter->val);
}
/* remove all items in the list */
while (!list_empty(&blist.list)) {
iter = list_entry(blist.list.next, struct a_list, list);
list_del(&iter->list);
free(iter);
}
return 0;
}