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

@ -29,27 +29,22 @@ public:
/* 出栈 */
int pop() {
if(stack.empty()){
throw out_of_range("栈为空,不能执行: pop()函数");
}
int oldTop = stack.back();
int oldTop = top();
stack.pop_back();
return oldTop;
}
/* 访问栈顶元素 */
int top() {
if(stack.empty()){
throw out_of_range("栈为空,不能执行: top()函数");
}
if(empty())
throw out_of_range("栈为空");
return stack.back();
}
/* 访问索引 index 处元素 */
int get(int index) {
if(stack.size() < index){
throw out_of_range("超出栈空间大小");
}
if(index >= size())
throw out_of_range("索引越界");
return stack[index];
}