Skip to content

Commit 254cb10

Browse files
committed
feat: 更新循环队列的实现
1 parent 6aafe82 commit 254cb10

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"editor.formatOnSave": true,
7878
"editor.tabSize": 4,
7979
"cSpell.words": [
80-
"inorder"
80+
"inorder",
81+
"stoi"
8182
]
8283
}

basic_data_structure/queue/queue.cpp

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2018-12-03 17:14:17
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2021-03-05 19:14:35
5+
* @Last Modified time: 2021-03-30 15:39:08
66
*/
77

88
#include <iostream>
@@ -11,7 +11,7 @@
1111
#include <stack>
1212
using namespace std;
1313

14-
// Normal Queue
14+
// 基本队列实现(Queue)
1515
class MyQueue
1616
{
1717
private:
@@ -57,23 +57,24 @@ class MyQueue
5757
}
5858
};
5959

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).
7576
*
76-
* Example:
77+
* 示例:
7778
* MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
7879
* circularQueue.enQueue(1); // return true
7980
* circularQueue.enQueue(2); // return true
@@ -85,8 +86,17 @@ class MyQueue
8586
* circularQueue.enQueue(4); // return true
8687
* circularQueue.Rear(); // return 4
8788
*
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+
*/
90100

91101
class MyCircularQueue
92102
{
@@ -97,7 +107,7 @@ class MyCircularQueue
97107
int size;
98108

99109
public:
100-
/** Initialize your data structure here. Set the size of the queue to be k. */
110+
/** 初始化数据结构,同时设置循环队列的长度 */
101111
MyCircularQueue(int k)
102112
{
103113
data.resize(k);
@@ -106,7 +116,7 @@ class MyCircularQueue
106116
size = k;
107117
}
108118

109-
/** Insert an element into the circular queue. Return true if the operation is successful. */
119+
/** 插入元素到循环队列,插入成功就返回 true,否则返回 false */
110120
bool enQueue(int value)
111121
{
112122
if (isFull())
@@ -123,7 +133,7 @@ class MyCircularQueue
123133
return true;
124134
}
125135

126-
/** Delete an element from the circular queue. Return true if the operation is successful. */
136+
/** 从循环队列删除元素,删除成功就返回 true,否则返回 false */
127137
bool deQueue()
128138
{
129139
if (isEmpty())
@@ -142,7 +152,7 @@ class MyCircularQueue
142152
return true;
143153
}
144154

145-
/** Get the front item from the queue. */
155+
/** 获取队首元素 */
146156
int Front()
147157
{
148158
if (isEmpty())
@@ -152,7 +162,7 @@ class MyCircularQueue
152162
return data[head];
153163
}
154164

155-
/** Get the last item from the queue. */
165+
/** 获取队尾元素 */
156166
int Rear()
157167
{
158168
if (isEmpty())
@@ -162,40 +172,23 @@ class MyCircularQueue
162172
return data[tail];
163173
}
164174

165-
/** Checks whether the circular queue is empty or not. */
175+
/** 检查循环队列是否为空. */
166176
bool isEmpty()
167177
{
168178
return head == -1;
169179
}
170180

171-
/** Checks whether the circular queue is full or not. */
181+
/** 检查循环队列是否已满 */
172182
bool isFull()
173183
{
174184
return ((tail + 1) % size) == head;
175185
}
176186
};
177187

178188
/**
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+
* 用两个栈来实现队列
196190
*
197-
* Example:
198-
* For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
191+
* 基本思路:LIFO + LIFO ==> FIFO
199192
*/
200193

201194
class MyQueueBy2Stacks

0 commit comments

Comments
 (0)