forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0225-implement-stack-using-queue.c
136 lines (120 loc) · 2.7 KB
/
0225-implement-stack-using-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
// A structure to represent an element of the queue.
typedef struct Node {
int value;
struct Node* next;
} Node;
typedef struct MyQueue {
int size;
Node* front;
Node* rear;
} MyQueue;
MyQueue* MyQueueCreate() {
MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
queue->size = 0;
queue->front = NULL;
queue->rear = NULL;
return queue;
}
void MyQueuePush(MyQueue** obj, int val) {
assert(obj);
assert(*obj);
MyQueue* queue = *obj;
Node* node = (Node*)malloc(sizeof(Node));
node->value = val;
node->next = NULL;
if (queue->rear) {
queue->rear->next = node;
queue->rear = node;
} else {
queue->front = node;
queue->rear = node;
}
queue->size++;
}
void MyQueuePop(MyQueue** obj) {
assert(obj);
assert(*obj);
MyQueue* queue = *obj;
Node* tmp = queue->front;
if (queue->size > 0 && tmp) {
queue->front = queue->front->next;
queue->size--;
free(tmp);
}
if (queue->rear == tmp) {
queue->rear = NULL;
}
}
int MyQueueFront(MyQueue** obj) {
assert(obj);
MyQueue* queue = *obj;
assert(queue->front);
return queue->front->value;
}
int MyQueueRear(MyQueue** obj) {
assert(obj);
MyQueue* queue = *obj;
assert(queue->rear);
return queue->rear->value;
}
int MyQueueSize(MyQueue** obj) {
assert(obj);
assert(*obj);
return (*obj)->size;
}
typedef struct {
MyQueue* queue;
} MyStack;
MyStack* myStackCreate() {
MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
stack->queue = NULL;
return stack;
}
void myStackPush(MyStack* obj, int x) {
assert(obj);
int size;
if (!obj->queue) {
obj->queue = MyQueueCreate();
}
MyQueuePush(&obj->queue, x);
size = obj->queue->size;
while(--size > 0) {
MyQueuePush(&obj->queue, MyQueueFront(&obj->queue));
MyQueuePop(&obj->queue);
}
}
int myStackPop(MyStack* obj) {
assert(obj);
int val = MyQueueFront(&obj->queue);
MyQueuePop(&obj->queue);
return val;
}
int myStackTop(MyStack* obj) {
assert(obj);
assert(obj->queue);
return MyQueueFront(&obj->queue);
}
bool myStackEmpty(MyStack* obj) {
assert(obj);
if (obj->queue) {
return obj->queue->size == 0;
}
return true;
}
void myStackFree(MyStack* obj) {
if(obj) {
while(obj->queue && obj->queue->front) {
MyQueuePop(&obj->queue);
}
free(obj);
}
}
/**
* Your MyStack struct will be instantiated and called as such:
* MyStack* obj = myStackCreate();
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/