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. 再识Quartz

    在之前的项目中使用过Quartz,但都是基于XML配置定义任务的.目前一个项目应用需要对任务进行创建.暂停.删除等动态管理.所以再次在网上翻了翻,再来好好重新认识下Quartz. 名词解释: sche ...

  2. 2.WHERE中使用=,>,>=,<,<=,<>,!=比较符号

        //查询工资大于等于2000的人   select * from person salary >= 2000;   //查询名字等于scott的人   select * from per ...

  3. Bridge桥接模式(设计模式11)

    在没有使用桥接模式: 扩展新问题(类归属膨胀问题) 1增加性的电脑类型,要增加每个品牌下面的类 2如果要增加一个新的电脑品牌,要增加美中电脑类型的类 违背单一职责原则: · 一个类:联想笔记本,有两个 ...

  4. python设计模式 之 简单工厂模式

    简单工厂模式属于类的创建型模式,适合用来对大量具有共同接口的类进行实例化,它能够推迟到执行的时候才动态决定要创建哪个类的实例.而不是在编译时就必须知道要实例化哪个类. python: #!/usr/b ...

  5. PHP第八课 字符串拆分经常使用函数

    课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...

  6. NOIP2017提高组 模拟赛13(总结)

    NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...

  7. MongoDB数据操作之删除与游标处理

    删除数据(比较常用) 范例:清空infos集合中的内容.表.文档.成员. db.infos.remove({"url":/-/}); 默认情况下都删除,第二个条件设为true,只删 ...

  8. SpringMVC+Spring+Hibernate框架整合原理,作用及使用方法

    转自:https://blog.csdn.net/bieleyang/article/details/77862042 SSM框架是spring MVC ,spring和mybatis框架的整合,是标 ...

  9. 如何将MVC AREA中的某一个页设为起始页

    public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...

  10. 关于markdown的使用

    首先: https://www.cnblogs.com/jordangong/p/9804777.html 注意:提交博客时需将 Markdown 源码粘贴到编辑器中,且编辑器没有实时预览,可以上传后 ...