Implement Stack using Queues 用队列实现栈
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
此时队列2中有元素。是可运行的容器。队列1是空,备用容器,所以我们须要两个表示符,代表两个队列,表示此队列是否是当前可操作的容器。简单来讲,就是用两个队列的操作(push()。pop(),empty())来实现栈的各种操作。
#include<iostream>
#include<queue>
using namespace std;
queue<int> qu1;
queue<int> qu2;
bool qu1_use=1;
bool qu2_use=0;
void push(int x);
void pop() ;
int top() ;
bool empty() ;
void main()
{
push(1);
push(2);
push(3);
push(4);
int i=5;
while(i)
{
if(!empty())
{
cout<<top()<<endl;
pop();
}
i--;
} }
void push(int x)
{
if(qu1_use==1)
{
qu1.push(x);
}
else
qu2.push(x);
}
void pop()
{
if(qu1.empty()&&qu2.empty())
exit(0);
if(qu1_use==1&&!qu1.empty())
{
while(qu1.size()>1)
{
qu2.push(qu1.front());
qu1.pop();
}
qu1.pop();
qu1_use=0;
qu2_use=1;
return;
}
if(qu2_use==1&&!qu2.empty())
{
while(qu2.size()>1)
{
qu1.push(qu2.front());
qu2.pop();
}
qu2.pop();
qu1_use=1;
qu2_use=0;
return;
}
return;
} int top()
{ if(qu1_use==1&&!qu1.empty())
{
return qu1.back();
}
if(qu2_use==1&&!qu2.empty())
{
return qu2.back();
}
//if(qu1.empty()&&qu2.empty())
exit(0);
} bool empty()
{
if((qu1_use==1&&qu1.empty())||(qu2_use==1&&qu2.empty()))
{
cout<<"empty!"<<endl;
return true;
} else return false;
}
运行上面代码得到结果为:
Implement Stack using Queues 用队列实现栈的更多相关文章
- LeetCode OJ:Implement Stack using Queues(队列实现栈)
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- Css学习总结(2)——60个有用CSS代码片段
1.垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,可以很优雅的解决这个困惑: .verticalcenter{ position: re ...
- 洛谷 P2243 电路维修
P2243 电路维修 题目背景 Elf 是来自Gliese 星球的少女,由于偶然的原因漂流到了地球上.在她无依无靠的时候,善良的运输队员Mark 和James 收留了她.Elf 很感谢Mark和Jam ...
- POJ 2154
这题的时间卡的.... 必须用欧拉来优化,而且要加素数表.最重要是,因为最后结果要/n,而数据很大,所以,必须在之前就先/n了,否则会爆数据. #include <iostream> #i ...
- Android多媒体学习六:利用Service实现背景音乐的播放
Android同意我们使用Service组件来完毕后台任务.这些任务的同意不会影响到用户其它的交互. 1.Activity类 [java] view plaincopy package demo.ca ...
- [CSS3] Image Width with sizes (srcset & sizes)
What if the image won't be displayed at the full viewport width? Then you need something more than s ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...
- 初识Dubbo 系列之5-Dubbo 成熟度
成熟度 功能成熟度 Feature特征 Maturity成熟度 Strength强度 Problem问题 Advise建议 User用户 并发控制 Tested 并发控制 试用 连接控制 Te ...
- 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo
GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...
- 闲来无事爬了下通讯录 试手 jsdom
curl http://xxx.com/address/addresslist\?search\=%40 --cookie oa_cookie=123 -s| node parss .js js 代码 ...
- ubuntu16.04安装破解pycharm
分两步,首先安装jdk,然后安装并破解pycharm 一.安装jdk 参考:http://blog.csdn.net/yebhweb/article/details/55098189 下载jdk1.8 ...