栈实现队列
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty())
while(stack1.size()>0)
{
int a=stack1.top();
stack1.pop();
stack2.push(a);
}
int b=stack2.top();
stack2.pop();
return b;
}
private:
stack<int> stack1;
stack<int> stack2;
};主要学习容器stack
stack 主要函数
empty:判断堆栈元素是否为空,true表示栈元素为空;
pop:移除栈顶元素;
push:栈顶添加元素;
top:返回栈顶元素;
size:返回栈中元素数目;
vivo公司福利 369人发布