-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode225.cpp
135 lines (115 loc) · 3.17 KB
/
LeetCode225.cpp
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
/*************************************************************************
> File Name: LeetCode225.cpp
> Author: ws
> Mail: [email protected]
> Created Time: 2020年02月27日 星期四 10时39分35秒
************************************************************************/
typedef struct {
int *data;
int head, tail;
int size, cnt;
} MyQueue;
MyQueue* myQueueCreate(int size) {
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
q->data = (int *)malloc(sizeof(int) * size);
q->size = size;
q->cnt = 0;
q->head = q->tail = 0;
return q;
}
/** Push element x onto stack. */
void myQueuePush(MyQueue* obj, int x) {
if (obj == NULL) return ;
if (obj->cnt == obj->size) return ;
obj->data[obj->tail++] = x;
if (obj->tail == obj->size) obj->tail -= obj->size;
obj->cnt += 1;
return ;
}
/** Removes the element on top of the stack and returns that element. */
int myQueuePop(MyQueue* obj) {
if (obj == NULL) return 0;
if (obj->cnt == 0) return 0;
obj->head += 1;
if (obj->head == obj->size) obj->head -= obj->size;
obj->cnt -= 1;
return 1;
}
/** Get the top element. */
int myQueueFront(MyQueue* obj) {
return obj->data[obj->head];
}
/** Returns whether the stack is empty. */
bool myQueueEmpty(MyQueue* obj) {
return obj->cnt == 0;
}
void myQueueFree(MyQueue* obj) {
if (obj == NULL) return ;
free(obj->data);
free(obj);
return ;
}
typedef struct {
MyQueue *q1, *q2;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
MyStack *s = (MyStack *)malloc(sizeof(MyStack));
s->q1 = myQueueCreate(100);
s->q2 = myQueueCreate(100);
return s;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
if (!myQueueEmpty(obj->q1)) {
myQueuePush(obj->q1, x);
} else {
myQueuePush(obj->q2, x);
}
return ;
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
MyQueue *p = myQueueEmpty(obj->q1) ? obj->q2 : obj->q1;
MyQueue *q = myQueueEmpty(obj->q1) ? obj->q1 : obj->q2;
int element = myQueueFront(p);
myQueuePop(p);
while (!myQueueEmpty(p)) {
myQueuePush(q, element);
element = myQueueFront(p);
myQueuePop(p);
}
return element;
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
MyQueue *p = myQueueEmpty(obj->q1) ? obj->q2 : obj->q1;
MyQueue *q = myQueueEmpty(obj->q1) ? obj->q1 : obj->q2;
int element;
while (!myQueueEmpty(p)) {
element = myQueueFront(p);
myQueuePop(p);
myQueuePush(q, element);
}
return element;
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return myQueueEmpty(obj->q1) && myQueueEmpty(obj->q2);
}
void myStackFree(MyStack* obj) {
if (obj == NULL) return ;
myQueueFree(obj->q1);
myQueueFree(obj->q2);
free(obj);
return ;
}
/**
* 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);
*/