2
2
* @Author: Chacha
3
3
* @Date: 2018-12-03 17:14:17
4
4
* @Last Modified by: Chacha
5
- * @Last Modified time: 2021-03-05 19:14:35
5
+ * @Last Modified time: 2021-03-30 15:39:08
6
6
*/
7
7
8
8
#include < iostream>
11
11
#include < stack>
12
12
using namespace std ;
13
13
14
- // Normal Queue
14
+ // 基本队列实现( Queue)
15
15
class MyQueue
16
16
{
17
17
private:
@@ -57,23 +57,24 @@ class MyQueue
57
57
}
58
58
};
59
59
60
- /* **********************************************************************************
61
- * Design your implementation of the circular queue.
62
- * The circular queue is a linear data structure in which the operations
63
- * are performed based on FIFO (First In First Out) principle and
64
- * the last position is connected back to the first position to make a circle.
65
- * It is also called "Ring Buffer".
66
- * Your implementation should support following operations:
67
- * 1. MyCircularQueue(k): Constructor, set the size of the queue to be k.
68
- * 2. Front: Get the front item from the queue. If the queue is empty, return -1.
69
- * 3. Rear: Get the last item from the queue. If the queue is empty, return -1.
70
- * 4. enQueue(value): Insert an element into the circular queue.
71
- * Return true if the operation is successful.
72
- * 5. deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
73
- * 6. isEmpty(): Checks whether the circular queue is empty or not.
74
- * 7. isFull(): Checks whether the circular queue is full or not.
60
+ /* *
61
+ * 设计循环队列。循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。
62
+ * 它也被称为“环形缓冲器”。
63
+ *
64
+ * 循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,
65
+ * 我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
66
+ *
67
+ * 实现要求:
68
+ * 1. MyCircularQueue(k): 构造器,设置队列长度为 k (Constructor, set the size of the queue to be k).
69
+ * 2. Front: 从队首获取元素,如果队列为空,返回 -1 (Get the front item from the queue. If the queue is empty, return -1).
70
+ * 3. Rear: 获取队尾元素。如果队列为空,返回 -1 (Get the last item from the queue. If the queue is empty, return -1).
71
+ * 4. enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真 (Insert an element into the circular queue.
72
+ * Return true if the operation is successful).
73
+ * 5. deQueue(): 从循环队列中删除一个元素 (Delete an element from the circular queue. Return true if the operation is successful).
74
+ * 6. isEmpty(): 检查循环队列是否为空 (Checks whether the circular queue is empty or not).
75
+ * 7. isFull(): 检查循环队列是否已满 (Checks whether the circular queue is full or not).
75
76
*
76
- * Example :
77
+ * 示例 :
77
78
* MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
78
79
* circularQueue.enQueue(1); // return true
79
80
* circularQueue.enQueue(2); // return true
@@ -85,8 +86,17 @@ class MyQueue
85
86
* circularQueue.enQueue(4); // return true
86
87
* circularQueue.Rear(); // return 4
87
88
*
88
- * Source: https://leetcode-cn.com/explore/learn/card/queue-stack/216/queue-first-in-first-out-data-structure/865/
89
- ************************************************************************************/
89
+ * 调用示例:
90
+ * MyCircularQueue obj = new MyCircularQueue(k);
91
+ * bool param_1 = obj.enQueue(value);
92
+ * bool param_2 = obj.deQueue();
93
+ * int param_3 = obj.Front();
94
+ * int param_4 = obj.Rear();
95
+ * bool param_5 = obj.isEmpty();
96
+ * bool param_6 = obj.isFull();
97
+ *
98
+ * 来源: https://leetcode-cn.com/leetbook/read/queue-stack/kzlb5/
99
+ */
90
100
91
101
class MyCircularQueue
92
102
{
@@ -97,7 +107,7 @@ class MyCircularQueue
97
107
int size;
98
108
99
109
public:
100
- /* * Initialize your data structure here. Set the size of the queue to be k. */
110
+ /* * 初始化数据结构,同时设置循环队列的长度 */
101
111
MyCircularQueue (int k)
102
112
{
103
113
data.resize (k);
@@ -106,7 +116,7 @@ class MyCircularQueue
106
116
size = k;
107
117
}
108
118
109
- /* * Insert an element into the circular queue. Return true if the operation is successful. */
119
+ /* * 插入元素到循环队列,插入成功就返回 true,否则返回 false */
110
120
bool enQueue (int value)
111
121
{
112
122
if (isFull ())
@@ -123,7 +133,7 @@ class MyCircularQueue
123
133
return true ;
124
134
}
125
135
126
- /* * Delete an element from the circular queue. Return true if the operation is successful. */
136
+ /* * 从循环队列删除元素,删除成功就返回 true,否则返回 false */
127
137
bool deQueue ()
128
138
{
129
139
if (isEmpty ())
@@ -142,7 +152,7 @@ class MyCircularQueue
142
152
return true ;
143
153
}
144
154
145
- /* * Get the front item from the queue. */
155
+ /* * 获取队首元素 */
146
156
int Front ()
147
157
{
148
158
if (isEmpty ())
@@ -152,7 +162,7 @@ class MyCircularQueue
152
162
return data[head];
153
163
}
154
164
155
- /* * Get the last item from the queue. */
165
+ /* * 获取队尾元素 */
156
166
int Rear ()
157
167
{
158
168
if (isEmpty ())
@@ -162,40 +172,23 @@ class MyCircularQueue
162
172
return data[tail];
163
173
}
164
174
165
- /* * Checks whether the circular queue is empty or not . */
175
+ /* * 检查循环队列是否为空 . */
166
176
bool isEmpty ()
167
177
{
168
178
return head == -1 ;
169
179
}
170
180
171
- /* * Checks whether the circular queue is full or not. */
181
+ /* * 检查循环队列是否已满 */
172
182
bool isFull ()
173
183
{
174
184
return ((tail + 1 ) % size) == head;
175
185
}
176
186
};
177
187
178
188
/* *
179
- * Your MyCircularQueue object will be instantiated and called as such:
180
- * MyCircularQueue obj = new MyCircularQueue(k);
181
- * bool param_1 = obj.enQueue(value);
182
- * bool param_2 = obj.deQueue();
183
- * int param_3 = obj.Front();
184
- * int param_4 = obj.Rear();
185
- * bool param_5 = obj.isEmpty();
186
- * bool param_6 = obj.isFull();
187
- */
188
-
189
- /* *
190
- * Implement Queue by Two Stacks
191
- * As the title described, you should only use two stacks to implement a queue's actions.
192
- * The queue should support push(element),
193
- * pop() and top() where pop is pop the first(a.k.a front) element in the queue.
194
- * Both pop and top methods should return the value of first element.
195
- * LIFO + LIFO ==> FIFO
189
+ * 用两个栈来实现队列
196
190
*
197
- * Example:
198
- * For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
191
+ * 基本思路:LIFO + LIFO ==> FIFO
199
192
*/
200
193
201
194
class MyQueueBy2Stacks
0 commit comments