-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathlist_test.c
71 lines (60 loc) · 1.31 KB
/
list_test.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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
typedef struct{
void *arg;
list_head list;
}st_header;
typedef struct{
void *arg1;
void *arg2;
void *arg3;
list_head list;
}st_struct;
void add_node(void *arg, list_head *head){
st_struct *s = (st_struct *)malloc(sizeof(st_struct));
s->arg1 = arg;
s->arg2 = arg;
s->arg3 = arg;
list_add(&(s->list), head);
}
void del_node(list_head *entry){
list_del(entry);
st_struct *s = list_entry(entry, st_struct, list);
free(s);
}
void display(list_head *head){
list_head *pos;
st_struct *et;
size_t i = 99;
list_for_each(pos, head) {
et = list_entry(pos, st_struct, list);
}
}
int main() {
st_header st_h;
// init list
INIT_LIST_HEAD(&st_h.list);
size_t i;
// add test
for (i = 0; i < 100; i++)
add_node((void *)i, &(st_h.list));
list_head *pos;
st_struct *et;
i = 99;
list_for_each(pos, &st_h.list) {
et = list_entry(pos, st_struct, list);
i--;
}
// delete test
for (i = 0; i < 32; i++) {
del_node(st_h.list.next);
}
i = 67;
list_for_each(pos, &st_h.list) {
et = list_entry(pos, st_struct, list);
printf("The num = %ld\n", (size_t)et->arg1);
i--;
}
return 0;
}