forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
225.c
174 lines (134 loc) · 3.48 KB
/
225.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/**
Implement Queue using Linked List.
*/
struct QueueNode {
int val;
struct QueueNode *next;
};
struct Queue{
int size;
struct QueueNode *front;
struct QueueNode *tail;
};
void push(struct Queue *queue, int new_val) {
if (queue == NULL) return;
struct QueueNode *new_node = (struct QueueNode *)malloc(sizeof(struct QueueNode));
new_node->val = new_val;
new_node->next = NULL;
if (queue->tail != NULL) {
queue->tail->next = new_node;
}
queue->tail = new_node;
if (queue->front == NULL) {
queue->front = new_node;
queue->size = 1;
}
else{
queue->size++;
}
}
void pop(struct Queue *queue) {
if (queue == NULL || queue->front == NULL) return;
struct QueueNode *tmp = queue->front;
queue->front = queue->front->next;
free(tmp);
queue->size--;
if (queue->front == NULL) {
queue->tail = NULL;
queue->size = 0;
}
}
int size(struct Queue *queue) {
if (queue == NULL) {
return 0;
}
else {
return queue->size;
}
}
bool isEmpty(struct Queue *queue) {
if (queue == NULL) {
return true;
}
else {
return (queue->size == 0) ? true : false;
}
}
/**
Implement Stack using Queues.
*/
typedef struct {
struct Queue queue;
} Stack;
/* Create a stack */
void stackCreate(Stack *stack, int maxSize) {
if (stack == NULL) return;
/* I'm a stack implemented using linked queue, so I don't need maxSize */
stack->queue.front = stack->queue.tail = 0;
stack->queue.size = 0;
}
/* Push element x onto stack */
void stackPush(Stack *stack, int element) {
if (stack == NULL) return;
push(&stack->queue, element);
}
/* Removes the element on top of the stack */
void stackPop(Stack *stack) {
if (stack == NULL) return;
int originalSize = size(&stack->queue);
int i;
for (i = 0; i < originalSize - 1; i++) {
if (stack->queue.front == NULL) {
return;
}
int tmp = stack->queue.front->val;
pop(&stack->queue);
push(&stack->queue, tmp);
}
pop(&stack->queue);
}
/* Get the top element */
int stackTop(Stack *stack) {
if (stack == NULL || stack->queue.tail == NULL) {
return 0;
}
else {
return stack->queue.tail->val;
}
}
/* Return whether the stack is empty */
bool stackEmpty(Stack *stack) {
if (stack == NULL) return true;
return isEmpty(&stack->queue);
}
/* Destroy the stack */
void stackDestroy(Stack *stack) {
if (stack == NULL) return;
while (size(&stack->queue) > 0) {
pop(&stack->queue);
}
}
int main() {
Stack s;
int maxSize = 5;
stackCreate(&s, maxSize);
printf("push 1 to stack.\n");
stackPush(&s, 1);
printf("push 2 to stack.\n");
stackPush(&s, 2);
printf("push 3 to stack.\n");
stackPush(&s, 3);
printf("current top of stack is: %d.\n", stackTop(&s));
printf("stack is empty? %d.\n", stackEmpty(&s));
printf("pop from stack.\n");
stackPop(&s);
printf("current top of stack is: %d.\n", stackTop(&s));
printf("destroy stack.\n");
stackDestroy(&s);
printf("current top of stack is: %d.\n", stackTop(&s));
printf("stack is empty? %d.\n", stackEmpty(&s));
return 0;
}