描述
实现一个栈,可以使用除了栈之外的数据结构
样例
例1:
输入:
push(1)
pop()
push(2)
top() // return 2
pop()
isEmpty() // return true
push(3)
isEmpty() // return false
例2:
输入:
isEmpty()
在这里插class Stack {
private:
vector<int> stack_vector; // 栈空间的指针
int m_iTop = 0; // 栈顶
public:
/*
* @param x: An integer
* @return: nothing
*/
void push(int x) {
stack_vector.push_back(x); //push_back() 在Vector最后添加一个元素(参数为要插入的值):
m_iTop++;
// write your code here
}
/*
* @return: nothing
*/
void pop() {
if (isEmpty())
return;
else
{
stack_vector.pop_back(); //删除Vector容器中的最后一个元素
m_iTop--;
}
// write your code here
}
/*
* @return: An integer
*/
int top() {
return stack_vector.back();//显示栈顶元素
// write your code here
}
/*
* @return: True if the stack is empty
*/
bool isEmpty() {
if (m_iTop == 0)//IsEmpty()是Java中用于判断某种容器是否有元素的系统库函数
{
return true;
}
else
{
return false;
}
// write your code here
}
};入代码片