mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update array stack.
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -57,7 +57,7 @@ class LinkedListQueue {
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
throw new IndexOutOfBoundsException();
|
||||
throw new EmptyStackException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
|
||||
@ -47,7 +47,7 @@ class LinkedListStack {
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (size() == 0)
|
||||
throw new IndexOutOfBoundsException();
|
||||
throw new EmptyStackException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user