This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
queue.c
142 lines (123 loc) · 3.91 KB
/
queue.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "basic.h"
#include "debug.h"
#include "vcpu.h"
void queue_remove(QUEUE *que){
que->prev->next = que->next;
que->next->prev = que->prev;
que->prev = NULL;
que->next = NULL;
}
void queue_insert(QUEUE *que, QUEUE *prev){
if(que==NULL||prev==NULL){
tv_abort("Null !!\n");
return;
}
que->next = prev->next;
que->next->prev = que;
que->prev = prev;
prev->next = que;
}
void queue_insert_prev(QUEUE *que, QUEUE *next){
if(que==NULL||next==NULL){
tv_abort("Null !\n");
return;
}
que->prev = next->prev;
que->prev->next = que;
que->next = next;
next->prev = que;
}
void queue_list_init(QUEUE_LIST *ls){
ls->head.prev = NULL;
ls->head.next = &(ls->tail);
ls->tail.prev = &(ls->head);
ls->tail.next = NULL;
}
void queue_list_enque(QUEUE_LIST *ls, QUEUE *que){
queue_insert(que, ls->tail.prev);
}
bool queue_list_is_empty(QUEUE_LIST *ls){
return ls->head.next->next==NULL;
}
QUEUE* queue_list_top(QUEUE_LIST *ls){
if(queue_list_is_empty(ls)) {
tv_abort("# pop failed\n");
return NULL;
}
QUEUE *que = ls->head.next;
return que;
}
QUEUE* queue_list_pop(QUEUE_LIST *ls){
if(queue_list_is_empty(ls)) {
tv_abort("# pop failed\n");
return NULL;
}
QUEUE *que = ls->head.next;
queue_remove(que);
return que;
}
bool queue_list_check(QUEUE_LIST *ls){
QUEUE* tmp = &(ls->head);
QUEUE* tail = &(ls->tail);
if(tmp->prev!=NULL) return false;
if(tail->next!=NULL) return false;
while(tmp->next!=NULL){
if(tmp->next->prev!=tmp) return false;
tmp = tmp->next;
if(tmp->prev->next!=tmp) return false;
}
return tail==tmp;
}
void queue_list_print(QUEUE_LIST *ls){
QUEUE *tmp=ls->head.next;
while(tmp!=&(ls->tail)){
if(tmp==tmp->next){
tv_abort("error break\n");
break;
}
tmp = tmp->next;
}
}
bool queue_list_find(QUEUE_LIST *ls, QUEUE *que){
QUEUE *tmp=ls->head.next;
while(tmp!=&(ls->tail)){
if(tmp==que) return true;
tmp = tmp->next;
}
return false;
}
void queue_test(void){
QUEUE que[4];
QUEUE_LIST ls;
queue_list_init(&ls);
if(!queue_list_check(&ls)) tv_error_message("init error\n");
queue_list_enque(&ls, &que[0]);
if(!queue_list_check(&ls)) tv_error_message("insert error\n");
if(!queue_list_find(&ls,&que[0])) tv_error_message("find error\n");
queue_list_enque(&ls, &que[1]);
if(!queue_list_check(&ls)) tv_error_message("insert error 2\n");
if(!queue_list_find(&ls,&que[1])) tv_error_message("find error1\n");
if(queue_list_pop(&ls)!=&que[0]) tv_error_message("pop elem error\n");
if(!queue_list_check(&ls)) tv_error_message("pop error\n");
if(queue_list_find(&ls,&que[0])) tv_error_message("find error0\n");
if(queue_list_pop(&ls)!=&que[1]) tv_error_message("pop elem error2\n");
if(!queue_list_check(&ls)) tv_error_message("pop error 2\n");
if(!queue_list_is_empty(&ls)) tv_error_message("empty check error 2\n");
queue_list_enque(&ls, &que[1]);
if(!queue_list_check(&ls)) tv_error_message("insert error 3\n");
queue_remove(&que[1]);
if(!queue_list_check(&ls)) tv_error_message("remove error\n");
if(!queue_list_is_empty(&ls)) tv_error_message("empty check error 3\n");
queue_list_enque(&ls, &que[1]);
if(!queue_list_check(&ls)) tv_error_message("insert error 4\n");
queue_remove(&que[1]);
if(!queue_list_check(&ls)) tv_error_message("remove error 2\n");
if(!queue_list_is_empty(&ls)) tv_error_message("empty check error 4\n");
queue_list_enque(&ls, &que[3]);
if(!queue_list_check(&ls)) tv_error_message("insert error 5\n");
queue_remove(&que[3]);
if(!queue_list_check(&ls)) tv_error_message("remove error 3\n");
if(!queue_list_is_empty(&ls)) tv_error_message("empty check error\n");
tv_error_message("test done\n");
tv_getc();
}