mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:26:30 +08:00
master
This commit is contained in:
parent
bbbed5b14b
commit
fe7564d54d
1 changed files with 9 additions and 0 deletions
|
@ -29,6 +29,9 @@ public:
|
|||
|
||||
/* 出栈 */
|
||||
int pop() {
|
||||
if(stack.empty()){
|
||||
throw out_of_range("栈为空,不能执行: pop()函数");
|
||||
}
|
||||
int oldTop = stack.back();
|
||||
stack.pop_back();
|
||||
return oldTop;
|
||||
|
@ -36,11 +39,17 @@ public:
|
|||
|
||||
/* 访问栈顶元素 */
|
||||
int top() {
|
||||
if(stack.empty()){
|
||||
throw out_of_range("栈为空,不能执行: top()函数");
|
||||
}
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if(stack.size() < index){
|
||||
throw out_of_range("超出栈空间大小");
|
||||
}
|
||||
return stack[index];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue