-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-232.cpp
More file actions
137 lines (113 loc) · 3.22 KB
/
LeetCode-232.cpp
File metadata and controls
137 lines (113 loc) · 3.22 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
//code 1
typedef struct {
int head, tail;
int *data;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue *m = (MyQueue *)malloc(sizeof(MyQueue));
m->data = (int *)malloc(sizeof(int) * 1000);
m->head = m->tail = 0;
return m;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
obj->data[obj->tail] = x;
// obj->size++;
obj->tail++;
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
int num = obj->data[obj->head];
obj->data[obj->head++] = 0;
return num;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
return obj->data[obj->head];
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return obj->head == obj->tail;
}
void myQueueFree(MyQueue* obj) {
if (obj == NULL) return ;
free(obj->data);
free(obj);
return ;
}
//code 2
//以下就是栈的一些基本操作
typedef struct MyStack {
int *data;
int top;
} MyStack;
MyStack *MyStackCreate(int size) {
MyStack *s = (MyStack *)malloc(sizeof(MyStack));
s->data = (int *)malloc(sizeof(int) * size);
s->top = -1;
return s;
}
void MyStackPush(MyStack *obj, int x) {
obj->data[++(obj->top)] = x;
}
int MyStackPop(MyStack *obj) {
return obj->data[(obj->top)--];
}
int MyStackTop(MyStack *obj) {
return obj->data[obj->top];
}
int MyStackEmpty(MyStack *obj) {
return obj->top == -1;
}
void MyStackFree(MyStack *obj) {
if (obj == NULL) return;
free(obj->data);
free(obj);
return ;
}
//以下就是用两个栈模拟队列先进先出功能
typedef struct {
MyStack *s1, *s2;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
int size = 1024;
MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));//队列的初始化操作
q->s1 = MyStackCreate(size);//栈初始化操作
q->s2 = MyStackCreate(size);
return q;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
MyStackPush(obj->s1, x);//我们把1栈作为入栈元素入口
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
if (MyStackEmpty(obj->s2)) {
while (!MyStackEmpty(obj->s1)) {
MyStackPush(obj->s2, MyStackPop(obj->s1));//如果1栈非空,且2栈空,那么就把1栈元素出栈放到2栈中,这样就实现了栈1的逆序排放
}
}
return MyStackPop(obj->s2);//栈2的栈顶元素就是对饮栈1栈底元素
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
if (MyStackEmpty(obj->s2)) {
while (!MyStackEmpty(obj->s1)) {
MyStackPush(obj->s2, MyStackPop(obj->s1));//与出队操作一样,将栈1逆序放到栈2中,栈2对应的栈顶元素就是栈1的栈底元素
}
}
return MyStackTop(obj->s2);
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
return MyStackEmpty(obj->s1) && MyStackEmpty(obj->s2);//两个栈同时为空则对应队列为空
}
void myQueueFree(MyQueue* obj) {
if (obj == NULL) return ;
MyStackFree(obj->s1);
MyStackFree(obj->s2);
free(obj);
return ;
}