Update array stack.

This commit is contained in:
Yudong Jin
2022-12-20 14:13:21 +08:00
parent fe7564d54d
commit fa3eff81d1
9 changed files with 46 additions and 43 deletions

View File

@ -33,16 +33,22 @@ class ArrayStack {
/* 出栈 */
public int pop() {
if (isEmpty())
throw new EmptyStackException();
return stack.remove(size() - 1);
}
/* 访问栈顶元素 */
public int peek() {
if (isEmpty())
throw new EmptyStackException();
return stack.get(size() - 1);
}
/* 访问索引 index 处元素 */
public int get(int index) {
if (index >= size())
throw new IndexOutOfBoundsException();
return stack.get(index);
}

View File

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

View File

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