forked from wg135/data-structure
/
queue2stacks.c
195 lines (137 loc) · 3.14 KB
/
queue2stacks.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#define EMPTY_TOS (-1)
typedef int element_type;
struct stack_record{
unsigned int stack_size;
int top_of_stack;
element_type *stack_array;
};
typedef struct stack_record *STACK;
struct queue_record{
STACK S1;
STACK S2;
};
typedef struct queue_record *QUEUE;
STACK init_stack(unsigned int max_elements);
QUEUE init_queue(unsigned int queue_size);
void dispose_stack(STACK S);
void dispose_queue(QUEUE Q);
int is_stack_empty(STACK S);
int is_stack_full(STACK S);
void push(element_type x, STACK S);
element_type pop(STACK S);
void enqueue(element_type x, QUEUE Q);
element_type dequeue(QUEUE Q);
int main(int argc, char *argv[]){
QUEUE Q;
int i;
Q = init_queue(8);
for(i=0; i<6; i++){
enqueue(i, Q);
}
for(i=0; i<6; i++){
printf("%d ", dequeue(Q));
}
printf("\n");
dispose_queue(Q);
return 0;
}
STACK init_stack(unsigned int max_elements){
STACK S;
S = (STACK)malloc(sizeof(struct stack_record));
if(S == NULL){
printf("Stack init malloc error\n");
exit(1);
}
S->stack_array = (element_type *)malloc(sizeof(element_type) * max_elements);
if(S->stack_array == NULL){
printf("stack array malloc error\n");
exit(1);
}
S->top_of_stack = EMPTY_TOS;
S->stack_size = max_elements;
return S;
}
QUEUE init_queue(unsigned int queue_size){
QUEUE Q;
if(queue_size == 0){
printf("invalid queue size\n");
exit(1);
}
Q = (QUEUE)malloc(sizeof(struct queue_record));
if(Q == NULL){
printf("queue init malloc error\n");
exit(1);
}
Q->S1 = init_stack(queue_size/2);
Q->S2 = init_stack(queue_size/2);
return Q;
}
void dispose_stack(STACK S){
if(S != NULL){
free(S->stack_array);
free(S);
}
}
void dispose_queue(QUEUE Q){
if(Q != NULL){
// dispose_stack(Q->S1);
//dispose_stack(Q->S2);
free(Q);
}
}
int is_stack_empty(STACK S){
return (S->top_of_stack == EMPTY_TOS);
}
int is_stack_full(STACK S){
return (S->stack_size == S->top_of_stack - 1);
}
void push(element_type x, STACK S){
if(is_stack_full(S)){
printf("stack is full\n");
exit(1);
}
else
S->stack_array[++S->top_of_stack] = x;
}
element_type pop(STACK S){
if(is_stack_empty(S)){
printf("stack empty\n");
exit(1);
}
else
return S->stack_array[S->top_of_stack--];
}
void enqueue(element_type x, QUEUE Q){
//both S1 and S2 are full
if(is_stack_full(Q->S1)&&(is_stack_full(Q->S2))){
printf("queue is full\n");
exit(1);
}
// S1 is full while S2 is not
if((is_stack_full(Q->S1))&&(!is_stack_full(Q->S2))){
while(!is_stack_full(Q->S2)){
push(pop(Q->S1), Q->S2);
}
push(x, Q->S1);
}
if((!is_stack_full(Q->S1))&&(!is_stack_full(Q->S2))){
push(x, Q->S1);
}
}
element_type dequeue(QUEUE Q){
// both S1 and S2 are empty
if((is_stack_empty(Q->S1))&&(is_stack_empty(Q->S2))){
printf("queue is empty\n");
exit(1);
}
if(!is_stack_empty(Q->S2))
return (pop(Q->S2));
if((!is_stack_empty(Q->S1)) && is_stack_empty(Q->S2)){
while(!is_stack_empty(Q->S1)){
push(pop(Q->S1), Q->S2);
}
return(pop(Q->S2));
}
}