mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Several enhancements and fixes
This commit is contained in:
@@ -78,7 +78,7 @@ class MaxHeap {
|
||||
}
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
bool empty() {
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ class MaxHeap {
|
||||
/* 元素出堆 */
|
||||
void pop() {
|
||||
// 判空处理
|
||||
if (empty()) {
|
||||
if (isEmpty()) {
|
||||
throw out_of_range("堆为空");
|
||||
}
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
@@ -149,7 +149,7 @@ int main() {
|
||||
cout << "\n堆元素数量为 " << size << endl;
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
bool isEmpty = maxHeap.empty();
|
||||
bool isEmpty = maxHeap.isEmpty();
|
||||
cout << "\n堆是否为空 " << isEmpty << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -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;
|
||||
|
||||
// 释放内存
|
||||
|
||||
@@ -77,34 +77,29 @@ public class LinkedListDeque {
|
||||
|
||||
/* 出队操作 */
|
||||
private int? pop(bool isFront) {
|
||||
// 若队列为空,直接返回 null
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
val = front.val; // 暂存头节点值
|
||||
// 删除头节点
|
||||
// 删除头节点
|
||||
ListNode fNext = front.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
|
||||
front = fNext; // 更新头节点
|
||||
}
|
||||
// 队尾出队操作
|
||||
else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
}
|
||||
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
|
||||
@@ -124,12 +119,16 @@ public class LinkedListDeque {
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int? peekFirst() {
|
||||
return isEmpty() ? null : front.val;
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int? peekLast() {
|
||||
return isEmpty() ? null : rear.val;
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
|
||||
@@ -53,7 +53,7 @@ class LinkedListQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0 || front == null)
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
@@ -35,9 +35,6 @@ class LinkedListStack {
|
||||
|
||||
/* 出栈 */
|
||||
public int pop() {
|
||||
if (stackPeek == null)
|
||||
throw new Exception();
|
||||
|
||||
int num = peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
@@ -46,7 +43,7 @@ class LinkedListStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0 || stackPeek == null)
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
@@ -72,10 +72,9 @@ class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
private Integer pop(boolean isFront) {
|
||||
// 若队列为空,直接返回 null
|
||||
private int pop(boolean isFront) {
|
||||
if (isEmpty())
|
||||
return null;
|
||||
throw new IndexOutOfBoundsException();
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
@@ -103,23 +102,27 @@ class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public Integer popFirst() {
|
||||
public int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public Integer popLast() {
|
||||
public int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public Integer peekFirst() {
|
||||
return isEmpty() ? null : front.val;
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public Integer peekLast() {
|
||||
return isEmpty() ? null : rear.val;
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
|
||||
@@ -55,7 +55,7 @@ class LinkedListQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class LinkedListStack {
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
@@ -62,9 +62,8 @@ class LinkedListDeque:
|
||||
|
||||
def pop(self, is_front: bool) -> int:
|
||||
"""出队操作"""
|
||||
# 若队列为空,直接返回 None
|
||||
if self.is_empty():
|
||||
return None
|
||||
raise IndexError("双向队列为空")
|
||||
# 队首出队操作
|
||||
if is_front:
|
||||
val: int = self.front.val # 暂存头节点值
|
||||
|
||||
Reference in New Issue
Block a user