mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-04 06:30:08 +08:00
@@ -29,18 +29,22 @@ public:
|
||||
|
||||
/* 出栈 */
|
||||
int pop() {
|
||||
int oldTop = stack.back();
|
||||
int oldTop = top();
|
||||
stack.pop_back();
|
||||
return oldTop;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if(empty())
|
||||
throw out_of_range("栈为空");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if(index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return stack[index];
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,16 +28,19 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index) {
|
||||
if (index >= this.size) throw "索引越界";
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
|
||||
@@ -27,14 +27,17 @@ class ArrayStack:
|
||||
|
||||
""" 出栈 """
|
||||
def pop(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack.pop()
|
||||
|
||||
""" 访问栈顶元素 """
|
||||
def peek(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack[-1]
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
def get(self, index):
|
||||
assert index < self.size(), "索引越界"
|
||||
return self.__stack[index]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
|
||||
@@ -11,6 +11,7 @@ class ArrayStack {
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size(): number {
|
||||
return this.stack.length;
|
||||
@@ -28,16 +29,19 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
pop(): number | undefined {
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top(): number | undefined {
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index: number): number | undefined {
|
||||
if (index >= this.size) throw new Error('索引越界');
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
@@ -83,4 +87,4 @@ console.log("栈的长度 size = " + size);
|
||||
const empty = stack.empty();
|
||||
console.log("栈是否为空 = " + empty);
|
||||
|
||||
export { };
|
||||
export { };
|
||||
|
||||
Reference in New Issue
Block a user