]>
Commit | Line | Data |
---|---|---|
dacf4783 JM |
1 | /* |
2 | * Doubly-linked list - test program | |
3 | * Copyright (c) 2009, Jouni Malinen <j@w1.fi> | |
4 | * | |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
dacf4783 JM |
7 | */ |
8 | ||
9 | #include "utils/includes.h" | |
10 | #include "utils/os.h" | |
11 | #include "utils/list.h" | |
12 | ||
13 | struct test { | |
14 | struct dl_list list; | |
15 | int value; | |
16 | }; | |
17 | ||
18 | static void dump_list(struct dl_list *head) | |
19 | { | |
20 | struct test *t; | |
21 | printf("dump:"); | |
22 | dl_list_for_each(t, head, struct test, list) | |
23 | printf(" %d", t->value); | |
24 | printf(" (len=%d%s)\n", dl_list_len(head), | |
25 | dl_list_empty(head) ? " empty" : ""); | |
26 | } | |
27 | ||
28 | int main(int argc, char *argv[]) | |
29 | { | |
30 | struct dl_list head; | |
31 | struct test *t, *tmp; | |
32 | int i; | |
33 | ||
34 | dl_list_init(&head); | |
35 | dump_list(&head); | |
36 | ||
37 | for (i = 0; i < 5; i++) { | |
38 | t = os_zalloc(sizeof(*t)); | |
39 | if (t == NULL) | |
40 | return -1; | |
41 | t->value = i; | |
42 | dl_list_add(&head, &t->list); | |
43 | dump_list(&head); | |
44 | } | |
45 | ||
46 | for (i = 10; i > 5; i--) { | |
47 | t = os_zalloc(sizeof(*t)); | |
48 | if (t == NULL) | |
49 | return -1; | |
50 | t->value = i; | |
51 | dl_list_add_tail(&head, &t->list); | |
52 | dump_list(&head); | |
53 | } | |
54 | ||
55 | i = 0; | |
56 | dl_list_for_each(t, &head, struct test, list) | |
57 | if (++i == 5) | |
58 | break; | |
59 | printf("move: %d\n", t->value); | |
60 | dl_list_del(&t->list); | |
61 | dl_list_add(&head, &t->list); | |
62 | dump_list(&head); | |
63 | ||
64 | dl_list_for_each_safe(t, tmp, &head, struct test, list) { | |
65 | printf("delete: %d\n", t->value); | |
66 | dl_list_del(&t->list); | |
67 | os_free(t); | |
68 | dump_list(&head); | |
69 | } | |
70 | ||
71 | return 0; | |
72 | } |