-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlist05.c
105 lines (85 loc) · 2.66 KB
/
list05.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
98
99
100
101
102
103
104
// SPDX-License-Identifier: GPL-2.0-only
/*
* test/algorithm/list05.c
* Linked List Test 05
*
* Copyright(C) Jung-JaeJoon <[email protected]> on the www.kernel.bz
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/list.h>
#include "test/debug.h"
static struct fox {
char name[20];
unsigned long tail_length;
unsigned long weight;
int is_fantastic;
struct list_head list;
};
static LIST_HEAD (fox_head);
void list_test05(void)
{
struct list_head *p, *pn;
struct fox *n, *fox;
int i;
char name[20];
for (i = 1; i <= 4; i++) {
fox = malloc(sizeof(struct fox));
sprintf(name, "fox[%d]", i);
strcpy(fox->name, name);
//INIT_LIST_HEAD(&fox->list);
list_add(&fox->list, &fox_head);
}
//fox[4] fox[3] fox[2] fox[1]
list_for_each(p, &fox_head) {
fox = list_entry(p, struct fox, list);
pr_out(stack_depth, "%s", fox->name);
}
pr_out(stack_depth, "\n");
//fox[4] fox[3] fox[2] fox[1]
list_for_each_safe(p, pn, &fox_head) {
fox = list_entry(p, struct fox, list);
pr_out(stack_depth, "%s", fox->name);
}
pr_out(stack_depth, "\n");
//fox[4] fox[3] fox[2] fox[1]
list_for_each_entry(fox, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
//fox[4] fox[3] fox[2] fox[1]
list_for_each_entry_safe(fox, n, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
//p = fox_head.next;
p = fox_head.next->next;
fox = list_entry(p, struct fox, list);
//fox[3] fox[2] fox[1]
list_for_each_entry_safe_from(fox, n, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
p = fox_head.next;
fox = list_entry(p, struct fox, list);
//fox[3] fox[2] fox[1]
list_for_each_entry_safe_continue(fox, n, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
p = fox_head.next;
fox = list_entry(p, struct fox, list);
//head->prev
//fox[1] fox[2] fox[3] fox[4]
list_for_each_entry_safe_reverse(fox, n, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
p = fox_head.prev;
fox = list_entry(p, struct fox, list);
//fox[2] fox[3] fox[4]
list_for_each_entry_continue_reverse(fox, &fox_head, list)
pr_out(stack_depth, "%s", fox->name);
pr_out(stack_depth, "\n");
list_for_each(p, &fox_head) {
fox = list_entry(p, struct fox, list);
free(fox);
}
//list_del_init(&fox_head);
}