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

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

View File

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

View File

@ -46,7 +46,7 @@ class LinkedListStack {
/* 访问栈顶元素 */
public int peek() {
if (size() == 0)
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}