-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path225_implement_stack_using_queues.c
130 lines (112 loc) · 3.07 KB
/
225_implement_stack_using_queues.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
#include <stdlib.h>
#include <assert.h>
typedef struct {
int *base;
int head;
int tail;
int size;
} Queue;
typedef struct {
Queue *queue1;
Queue *queue2;
} Stack;
Queue *queueCreate(int maxSize) {
Queue *queue = (Queue *)malloc(sizeof(Queue));
queue->base = (int *)malloc(sizeof(int)*(maxSize+1));
queue->size = maxSize;
queue->head = 0;
queue->tail = 0;
return queue;
}
int queueEmpty(Queue *queue) {
return queue->head == queue->tail;
}
int queueSize(Queue *queue) {
return queue->tail-queue->head;
}
void queuePush(Queue *queue, int element) {
queue->base[queue->tail++] = element;
}
int queuePop(Queue *queue) {
return queue->base[queue->head++];
}
void queueDestroy(Queue *queue) {
free(queue->base);
free(queue);
}
/* Create a stack */
void stackCreate(Stack *stack, int maxSize) {
stack->queue1 = queueCreate(maxSize);
stack->queue2 = queueCreate(maxSize);
}
/* Push element x onto stack */
void stackPush(Stack *stack, int element) {
if(queueEmpty(stack->queue2))
queuePush(stack->queue1, element);
else
queuePush(stack->queue2, element);
}
/* Removes the element on top of the stack */
void stackPop(Stack *stack) {
if(queueEmpty(stack->queue2)) {
while(queueSize(stack->queue1) > 1) {
int element = queuePop(stack->queue1);
queuePush(stack->queue2, element);
}
queuePop(stack->queue1);
}
else if(queueEmpty(stack->queue1)) {
while(queueSize(stack->queue2) > 1) {
queuePush(stack->queue1, queuePop(stack->queue2));
}
queuePop(stack->queue2);
}
}
/* Get the top element */
int stackTop(Stack *stack) {
if(queueEmpty(stack->queue1) && queueEmpty(stack->queue2))
return 0;
if(queueEmpty(stack->queue2)) {
while(queueSize(stack->queue1) > 1) {
queuePush(stack->queue2, queuePop(stack->queue1));
}
int element = queuePop(stack->queue1);
queuePush(stack->queue2, element);
return element;
}
else {
while(queueSize(stack->queue2) > 1) {
queuePush(stack->queue1, queuePop(stack->queue2));
}
int element = queuePop(stack->queue2);
queuePush(stack->queue1, element);
return element;
}
}
/* Return whether the stack is empty */
int stackEmpty(Stack *stack) {
return queueEmpty(stack->queue1) && queueEmpty(stack->queue2);
}
int stackSize(Stack *stack) {
int queueSize1 = queueSize(stack->queue1);
int queueSize2 = queueSize(stack->queue2);
return queueSize1>queueSize2?queueSize1:queueSize2;
}
/* Destroy the stack */
void stackDestroy(Stack *stack) {
queueDestroy(stack->queue1);
queueDestroy(stack->queue2);
}
int main() {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stackCreate(stack, 100);
assert(stackEmpty(stack) == 1);
stackPush(stack, 1);
assert(stackSize(stack) == 1);
assert(stackEmpty(stack) == 0);
assert(stackTop(stack) == 1);
stackPop(stack);
assert(stackEmpty(stack) == 1);
free(stack);
return 0;
}