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.
解题思路:
用队列实现栈的操作。包含输入、输出、取栈顶元素、推断栈是否为空。
我们知道,栈是先进后出的容器,队列是先进先出的容器。所以输入元素非常easy,可是输出元素不是输出队列的front,而是back元素。删掉back元素。此时我们能够将队列1中的非队尾元素存在另外一个队列2中,删掉之前队列的最后一个元素不另行存储即可。

此时队列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;
}

运行上面代码得到结果为:

4
3
2
1
empty!

Implement Stack using Queues 用队列实现栈的更多相关文章

  1. LeetCode OJ:Implement Stack using Queues(队列实现栈)

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  2. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

  3. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  4. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  6. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  7. 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 ...

  8. 【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( ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. Css学习总结(2)——60个有用CSS代码片段

    1.垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,可以很优雅的解决这个困惑: .verticalcenter{ position: re ...

  2. 洛谷 P2243 电路维修

    P2243 电路维修 题目背景 Elf 是来自Gliese 星球的少女,由于偶然的原因漂流到了地球上.在她无依无靠的时候,善良的运输队员Mark 和James 收留了她.Elf 很感谢Mark和Jam ...

  3. POJ 2154

    这题的时间卡的.... 必须用欧拉来优化,而且要加素数表.最重要是,因为最后结果要/n,而数据很大,所以,必须在之前就先/n了,否则会爆数据. #include <iostream> #i ...

  4. Android多媒体学习六:利用Service实现背景音乐的播放

    Android同意我们使用Service组件来完毕后台任务.这些任务的同意不会影响到用户其它的交互. 1.Activity类 [java] view plaincopy package demo.ca ...

  5. [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 ...

  6. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...

  7. 初识Dubbo 系列之5-Dubbo 成熟度

    成熟度 功能成熟度 Feature特征 Maturity成熟度 Strength强度 Problem问题 Advise建议 User用户 并发控制 Tested 并发控制   试用   连接控制 Te ...

  8. 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo

    GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...

  9. 闲来无事爬了下通讯录 试手 jsdom

    curl http://xxx.com/address/addresslist\?search\=%40 --cookie oa_cookie=123 -s| node parss .js js 代码 ...

  10. ubuntu16.04安装破解pycharm

    分两步,首先安装jdk,然后安装并破解pycharm 一.安装jdk 参考:http://blog.csdn.net/yebhweb/article/details/55098189 下载jdk1.8 ...