mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Several enhancements and fixes
This commit is contained in:
@ -37,7 +37,7 @@ class ArrayQueue {
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ class ArrayQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek() {
|
||||
if (empty())
|
||||
if (isEmpty())
|
||||
throw out_of_range("队列为空");
|
||||
return nums[front];
|
||||
}
|
||||
@ -110,7 +110,7 @@ int main() {
|
||||
cout << "队列长度 size = " << size << endl;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty = queue->empty();
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "队列是否为空 = " << empty << endl;
|
||||
|
||||
/* 测试环形数组 */
|
||||
|
||||
@ -18,8 +18,8 @@ class ArrayStack {
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
bool empty() {
|
||||
return stack.empty();
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
@ -35,7 +35,7 @@ class ArrayStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if (empty())
|
||||
if (isEmpty())
|
||||
throw out_of_range("栈为空");
|
||||
return stack.back();
|
||||
}
|
||||
@ -74,7 +74,7 @@ int main() {
|
||||
cout << "栈的长度 size = " << size << endl;
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool empty = stack->empty();
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "栈是否为空 = " << empty << endl;
|
||||
|
||||
// 释放内存
|
||||
|
||||
@ -81,9 +81,8 @@ class LinkedListDeque {
|
||||
|
||||
/* 出队操作 */
|
||||
int pop(bool isFront) {
|
||||
// 若队列为空,直接返回 -1
|
||||
if (isEmpty())
|
||||
return -1;
|
||||
throw out_of_range("队列为空");
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
@ -124,12 +123,16 @@ class LinkedListDeque {
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peekFirst() {
|
||||
return isEmpty() ? -1 : front->val;
|
||||
if (isEmpty())
|
||||
throw out_of_range("双向队列为空");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
int peekLast() {
|
||||
return isEmpty() ? -1 : rear->val;
|
||||
if (isEmpty())
|
||||
throw out_of_range("双向队列为空");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
|
||||
@ -30,7 +30,7 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ int main() {
|
||||
cout << "队列长度 size = " << size << endl;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool empty = queue->empty();
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "队列是否为空 = " << empty << endl;
|
||||
|
||||
// 释放内存
|
||||
|
||||
@ -29,7 +29,7 @@ class LinkedListStack {
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ class LinkedListStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw out_of_range("栈为空");
|
||||
return stackTop->val;
|
||||
}
|
||||
@ -98,7 +98,7 @@ int main() {
|
||||
cout << "栈的长度 size = " << size << endl;
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool empty = stack->empty();
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "栈是否为空 = " << empty << endl;
|
||||
|
||||
// 释放内存
|
||||
|
||||
Reference in New Issue
Block a user