用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution
{
public:
void push(int node) {
stack1.push(node);//将node压入stack1中
}
int pop() {
int ref;//定义ref为记录出栈的元素
if(!stack2.empty())//记录stack2不为空时的情况
{
ref = stack2.top();
stack2.pop();
}
else{
if(!stack1.empty())//记录stack2为空,但是stack1不为空时的情况
{
while(!stack1.empty())
{
int temp;
temp = stack1.top();
stack2.push(temp);
stack1.pop();
}
ref = stack2.top();
stack2.pop();
}
}
return ref;//返回pop出的元素值
}
private:
stack<int> stack1;
stack<int> stack2;
};
本文地址:https://blog.csdn.net/weixin_42534963/article/details/107354973
如对本文有疑问, 点击进行留言回复!!
before社区电量是什么意思 Before社区电量获得方法
RecycleView入门详解(教你全面掌握RecycleView用法)
动态权限请求框架RxPermissions(几行代码搞定权限)
URL路径@PathVariable出现点号“.“时值遭截断问题
网友评论