-
Notifications
You must be signed in to change notification settings - Fork 0
/
circleq.c
77 lines (64 loc) · 2.04 KB
/
circleq.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
int main()
{
CIRCLEQ_HEAD(circleq, entry) head;
struct circleq *headp; /* Circular queue head. */
struct entry {
int a;
int b;
CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
} *n1, *n2, *n3, *np, lookup_table[10];
CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
// filling lookup_table
int i;
for(i = 0; i < 10; i++) {
lookup_table[i].a = lookup_table[i].b = i + 10;
CIRCLEQ_INSERT_TAIL(&head, &lookup_table[i], entries);
}
n1 = malloc(sizeof(struct entry));
n1->a = 1;
n1->b = 1;
CIRCLEQ_INSERT_TAIL(&head, n1, entries);
n2 = malloc(sizeof(struct entry));
n2->a = 2;
n2->b = 2;
CIRCLEQ_INSERT_TAIL(&head, n2, entries);
n3 = malloc(sizeof(struct entry));
n3->a = 3;
n3->b = 3;
CIRCLEQ_INSERT_TAIL(&head, n3, entries);
struct entry *p = CIRCLEQ_FIRST(&head);
p = CIRCLEQ_LOOP_NEXT(&head, n1, entries);
//printf("a:%d, b:%d\n", p->a, p->b);
p = CIRCLEQ_LOOP_NEXT(&head, n2, entries);
//printf("a:%d, b:%d\n", p->a, p->b);
p = CIRCLEQ_LOOP_NEXT(&head, n3, entries);
//printf("a:%d, b:%d\n", p->a, p->b);
printf("orig: %p, %p\n", lookup_table[5].entries.cqe_next,
lookup_table[5].entries.cqe_prev);
CIRCLEQ_REMOVE(&head, &lookup_table[5], entries);
printf("after: %p, %p\n", lookup_table[5].entries.cqe_next,
lookup_table[5].entries.cqe_prev);
for(i = 0; i < 30; i++) {
p = CIRCLEQ_LOOP_NEXT(&head, p, entries);
printf("a:%d, b:%d\n", p->a, p->b);
}
puts("---------");
#if 0
/* Forward traversal. */
for (np = head.cqh_first; np != (void *)&head;
np = np->entries.cqe_next) {
printf("a:%d, b:%d\n", np->a, np->b);
}
/* Reverse traversal. */
for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev) {
printf("a:%d, b:%d\n", np->a, np->b);
}
/* Delete. */
while (head.cqh_first != (void *)&head)
CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
#endif
return 0;
}