首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
用两个栈实现一个队列的功能?
[问答题]
用两个栈实现一个队列的功能?
添加笔记
求解答(0)
邀请回答
收藏(14)
分享
纠错
2个回答
添加回答
4
Nickqiao
两个栈stack1,stack2,入队时压入stack1,出队时将stack1中的元素弹出压入stack2中,此时元素恢复原来顺序,将stack2栈顶弹出,完成出队(当stack2不为空时直接弹出stack2栈顶元素)。
class
Solution
{
public
:
void
push(
int
node) {
stack1.push(node);
}
int
pop() {
int
a;
if
(stack2.empty()){
while
(!stack1.empty()){
a=stack1.top();
stack2.push(a);
stack1.pop();
}
}
a=stack2.top();
stack2.pop();
return
a;
}
private
:
stack<
int
> stack1;
stack<
int
> stack2;
};
编辑于 2017-01-22 16:43:48
回复(0)
0
架构师的老师
链接:
https://www.nowcoder.com/questionTerminal/61e50a60279c48848c06de24325611d1
来源:牛客网
class Solution
{
public :
void push( int node) {
stack1.push(node);
}
int pop() {
int a;
if (stack2.empty()){
while (!stack1.empty()){
a=stack1.top();
stack2.push(a);
stack1.pop();
}
}
a=stack2.top();
stack2.pop();
return a;
}
private :
stack<int > stack1;
stack<int > stack2;
};
发表于 2019-03-16 11:19:50
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
栈
队列
上传者:
牛100
难度:
2条回答
14收藏
2316浏览
热门推荐
相关试题
明明的随机数
数组
评论
(3692)
来自
华为研发工程师编程题
已知a
40
=...
京东
职能
2019
财务
保险
评论
(1)
《拳皇97》最后BOSS是谁?
游戏常识
评论
(1)
《魔兽世界》中,下列不属于玩家可以...
游戏常识
评论
(1)
有20000人的就餐需求,现建了一...
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题