-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-225.cpp
More file actions
173 lines (148 loc) · 4.43 KB
/
LeetCode-225.cpp
File metadata and controls
173 lines (148 loc) · 4.43 KB
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
//code 1
typedef struct {
int *data;
int head, tail, size;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
MyStack *m = (MyStack *)malloc(sizeof(MyStack));
m->data = (int *)malloc(sizeof(int) * 1000);
m->head = m->tail = m->size = 0;
return m;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
for (int i = obj->size ; i >= 1; i--) {
obj->data[i] = obj->data[i - 1];
}
obj->data[0] = x;
obj->size++;
}
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
int num = obj->data[0];
for (int i = 1; i < obj->size; i++) {
obj->data[i - 1] = obj->data[i];
}
obj->size--;
return num;
}
/** Get the top element. */
int myStackTop(MyStack* obj) {
return obj->data[0];
}
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
return obj->size == 0;
}
void myStackFree(MyStack* obj) {
if (obj == NULL) return ;
free(obj->data);
free(obj);
return ;
}
// code 2
//使用两个队列模拟栈的先进后出性质
typedef struct MyQueue{
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->head = q->tail = 0;
q->cnt = 0;
q->size = size;
return q;
}
//入队操作
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 ;
}
//出队操作
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;
}
//队首元素输出
int MyQueueFront(MyQueue *obj) {
return obj->data[obj->head];
}
//队列判空操作
int 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() {
int size =1024;//由于要初始化两个队列空间,暂时设定队列大小为1024
MyStack *s = (MyStack *)malloc(sizeof(MyStack));
s->q1 = MyQueueCreate(size);
s->q2 = MyQueueCreate(size);
return s;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
if (!MyQueueEmpty(obj->q1)) {
MyQueuePush(obj->q1, x);//判断栈1否为空,为空则直接插入到栈1中
} else {
MyQueuePush(obj->q2, x);//否则插入到队列2中,便于弹栈
}
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;//如果队列1为空p指向队列2
MyQueue *q = MyQueueEmpty(obj->q1) ? obj->q1 : obj->q2;//如果队列1为空q指向队列1
int element = MyQueueFront(p);//对应栈顶元素就是对应队列p的头元素,记录栈顶元素
MyQueuePop(p);//栈顶元素出栈
while (!MyQueueEmpty(p)) {//把队列p中的元素全部放到队列q中去
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);//把队列p中元素全部转移到队列q中去,并且释放队列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 ;
}