mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 11:26:29 +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() {
|
int pop() {
|
||||||
|
if(stack.empty()){
|
||||||
|
throw out_of_range("栈为空,不能执行: pop()函数");
|
||||||
|
}
|
||||||
int oldTop = stack.back();
|
int oldTop = stack.back();
|
||||||
stack.pop_back();
|
stack.pop_back();
|
||||||
return oldTop;
|
return oldTop;
|
||||||
|
@ -36,11 +39,17 @@ public:
|
||||||
|
|
||||||
/* 访问栈顶元素 */
|
/* 访问栈顶元素 */
|
||||||
int top() {
|
int top() {
|
||||||
|
if(stack.empty()){
|
||||||
|
throw out_of_range("栈为空,不能执行: top()函数");
|
||||||
|
}
|
||||||
return stack.back();
|
return stack.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问索引 index 处元素 */
|
/* 访问索引 index 处元素 */
|
||||||
int get(int index) {
|
int get(int index) {
|
||||||
|
if(stack.size() < index){
|
||||||
|
throw out_of_range("超出栈空间大小");
|
||||||
|
}
|
||||||
return stack[index];
|
return stack[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue