Several enhancements and fixes

This commit is contained in:
krahets
2023-09-14 03:36:31 +08:00
parent a9d70e9e4b
commit d190dbf3c1
18 changed files with 84 additions and 70 deletions

View File

@@ -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;
}
/* 返回数组用于打印 */

View File

@@ -53,7 +53,7 @@ class LinkedListQueue {
/* 访问队首元素 */
public int peek() {
if (size() == 0 || front == null)
if (isEmpty())
throw new Exception();
return front.val;
}

View File

@@ -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;
}