1
1
/* *
2
- * @Author: Chacha
3
- * @Date: 2018-12-03 17:14:17
2
+ * @Author: Chacha
3
+ * @Date: 2018-12-03 17:14:17
4
4
* @Last Modified by: Chacha
5
5
* @Last Modified time: 2018-12-12 21:50:07
6
6
*/
@@ -21,16 +21,21 @@ struct TreeNode {
21
21
class MyQueue {
22
22
private:
23
23
// store elements
24
- vector<int > data;
24
+ vector<int > data;
25
25
// a pointer to indicate the start position
26
- int p_start;
26
+ int p_start;
27
+
27
28
public:
28
- MyQueue () {p_start = 0 ;}
29
+ MyQueue () {
30
+ p_start = 0 ;
31
+ }
32
+
29
33
/* * Insert an element into the queue. Return true if the operation is successful. */
30
34
bool enQueue (int x) {
31
35
data.push_back (x);
32
36
return true ;
33
37
}
38
+
34
39
/* * Delete an element from the queue. Return true if the operation is successful. */
35
40
bool deQueue () {
36
41
if (isEmpty ()) {
@@ -39,32 +44,34 @@ class MyQueue {
39
44
p_start++;
40
45
return true ;
41
46
};
47
+
42
48
/* * Get the front item from the queue. */
43
49
int Front () {
44
50
return data[p_start];
45
51
};
52
+
46
53
/* * Checks whether the queue is empty or not. */
47
54
bool isEmpty () {
48
55
return p_start >= data.size ();
49
56
}
50
57
};
51
58
52
59
/* **********************************************************************************
53
- * Design your implementation of the circular queue.
54
- * The circular queue is a linear data structure in which the operations
55
- * are performed based on FIFO (First In First Out) principle and
56
- * the last position is connected back to the first position to make a circle.
60
+ * Design your implementation of the circular queue.
61
+ * The circular queue is a linear data structure in which the operations
62
+ * are performed based on FIFO (First In First Out) principle and
63
+ * the last position is connected back to the first position to make a circle.
57
64
* It is also called "Ring Buffer".
58
65
* Your implementation should support following operations:
59
66
* 1. MyCircularQueue(k): Constructor, set the size of the queue to be k.
60
67
* 2. Front: Get the front item from the queue. If the queue is empty, return -1.
61
68
* 3. Rear: Get the last item from the queue. If the queue is empty, return -1.
62
- * 4. enQueue(value): Insert an element into the circular queue.
69
+ * 4. enQueue(value): Insert an element into the circular queue.
63
70
* Return true if the operation is successful.
64
71
* 5. deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
65
72
* 6. isEmpty(): Checks whether the circular queue is empty or not.
66
73
* 7. isFull(): Checks whether the circular queue is full or not.
67
- *
74
+ *
68
75
* Example:
69
76
* MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
70
77
* circularQueue.enQueue(1); // return true
@@ -76,7 +83,7 @@ class MyQueue {
76
83
* circularQueue.deQueue(); // return true
77
84
* circularQueue.enQueue(4); // return true
78
85
* circularQueue.Rear(); // return 4
79
- *
86
+ *
80
87
* Source: https://leetcode-cn.com/explore/learn/card/queue-stack/216/queue-first-in-first-out-data-structure/865/
81
88
************************************************************************************/
82
89
@@ -95,7 +102,7 @@ class MyCircularQueue {
95
102
tail = -1 ;
96
103
size = k;
97
104
}
98
-
105
+
99
106
/* * Insert an element into the circular queue. Return true if the operation is successful. */
100
107
bool enQueue (int value) {
101
108
if (isFull ()) {
@@ -109,44 +116,44 @@ class MyCircularQueue {
109
116
data[tail] = value;
110
117
return true ;
111
118
}
112
-
119
+
113
120
/* * Delete an element from the circular queue. Return true if the operation is successful. */
114
121
bool deQueue () {
115
122
if (isEmpty ()) {
116
123
return false ;
117
124
}
118
-
125
+
119
126
if (head == tail) {
120
127
head = -1 ;
121
128
tail = -1 ;
122
129
return true ;
123
130
}
124
-
131
+
125
132
head = (head + 1 ) % size;
126
133
return true ;
127
134
}
128
-
135
+
129
136
/* * Get the front item from the queue. */
130
137
int Front () {
131
138
if (isEmpty ()) {
132
139
return -1 ;
133
140
}
134
141
return data[head];
135
142
}
136
-
143
+
137
144
/* * Get the last item from the queue. */
138
145
int Rear () {
139
146
if (isEmpty ()) {
140
147
return -1 ;
141
148
}
142
149
return data[tail];
143
150
}
144
-
151
+
145
152
/* * Checks whether the circular queue is empty or not. */
146
153
bool isEmpty () {
147
154
return head == -1 ;
148
155
}
149
-
156
+
150
157
/* * Checks whether the circular queue is full or not. */
151
158
bool isFull () {
152
159
return ((tail + 1 ) % size) == head;
@@ -168,11 +175,11 @@ class MyCircularQueue {
168
175
/* *
169
176
* Implement Queue by Two Stacks
170
177
* As the title described, you should only use two stacks to implement a queue's actions.
171
- * The queue should support push(element),
178
+ * The queue should support push(element),
172
179
* pop() and top() where pop is pop the first(a.k.a front) element in the queue.
173
180
* Both pop and top methods should return the value of first element.
174
181
* LIFO + LIFO ==> FIFO
175
- *
182
+ *
176
183
* Example:
177
184
* For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
178
185
*/
@@ -189,7 +196,7 @@ class MyQueue {
189
196
void push (int element) {
190
197
stack1.push (element);
191
198
}
192
-
199
+
193
200
void adjust () {
194
201
if (stack2.empty ()) {
195
202
while (!stack1.empty ()) {
@@ -198,7 +205,7 @@ class MyQueue {
198
205
}
199
206
}
200
207
}
201
-
208
+
202
209
int pop () {
203
210
adjust ();
204
211
int temp = stack2.top ();
@@ -215,9 +222,9 @@ class MyQueue {
215
222
/* *
216
223
* BFS - Template 1
217
224
* Return the length of the shortest path between root and target node.
218
- *
225
+ *
219
226
* 1. As shown in the code, in each round, the nodes in the queue are the nodes which are waiting to be processed.
220
- * 2. After each outer while loop, we are one step farther from the root node.
227
+ * 2. After each outer while loop, we are one step farther from the root node.
221
228
* The variable step indicates the distance from the root node and the current node we are visiting.
222
229
*/
223
230
int BFS (TreeNode root, TreeNode target) {
@@ -245,7 +252,7 @@ int BFS(TreeNode root, TreeNode target) {
245
252
/* *
246
253
* BFS - Template 2
247
254
* Return the length of the shortest path between root and target node.
248
- *
255
+ *
249
256
* There are two cases you don't need the hash set used:
250
257
* 1. You are absolutely sure there is no cycle, for example, in tree traversal;
251
258
* 2. You do want to add the node to the queue multiple times.
@@ -287,4 +294,4 @@ int main() {
287
294
if (!q.isEmpty ()) {
288
295
cout << q.Front () << endl;
289
296
}
290
- }
297
+ }
0 commit comments