Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack -- which means only push
    to top
    peek/pop from topsize,
    and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

  • 题目解析:
  • 用堆栈stack实现队列,仅仅能使用堆栈的操作,push(),pop()。empty().

  • 方法:
  • 用两个堆栈实现。代码例如以下:
  • class Queue {
    public:
    stack<int> st1;
    stack<int> st2;
    bool st1_use=1;
    bool st2_use=0;
    // Push element x to the back of queue.
    void push(int x) {
    if(st1_use==1)
    st1.push(x);
    else if(st2_use==1)
    st2.push(x);
    } // Removes the element from in front of queue.
    void pop(void) {
    if(st1.empty()&&st2.empty())
    exit(0);
    if(st1_use==1&&st2_use==0)
    {
    while(st1.size()>1)
    {
    int temp=st1.top();
    st2.push(temp);
    st1.pop();
    }
    st1.pop();
    while(st2.size()>0)
    {
    int temp=st2.top();
    st1.push(temp);
    st2.pop();
    }
    st1_use==1;
    st2_use==0;
    return;
    }
    if(st1_use==0&&st2_use==1)
    {
    while(st2.size()>1)
    {
    int temp=st2.top();
    st1.push(temp);
    st2.pop();
    }
    st2.pop();
    while(st1.size()>0)
    {
    int temp=st1.top();
    st2.push(temp);
    st1.pop();
    }
    st1_use==0;
    st2_use==1;
    return;
    }
    } // Get the front element.
    int peek(void) {
    if(st1_use==1&&st2_use==0)
    {
    int temp;
    while(st1.size()>0)
    {
    temp=st1.top();
    st2.push(temp);
    st1.pop();
    } while(st2.size()>0)
    {
    int temp1=st2.top();
    st1.push(temp1);
    st2.pop();
    }
    st1_use==1;
    st2_use==0;
    return temp;
    }
    if(st1_use==0&&st2_use==1)
    {
    int temp;
    while(st2.size()>0)
    {
    int temp=st2.top();
    st1.push(temp);
    st2.pop();
    } while(st1.size()>0)
    {
    int temp1=st1.top();
    st2.push(temp1);
    st1.pop();
    }
    st1_use==0;
    st2_use==1;
    return temp;
    }
    } // Return whether the queue is empty.
    bool empty(void) {
    if(st1_use==1&&st1.empty())
    return true;
    if(st2_use==1&&st2.empty())
    return true;
    return false;
    } };

Implement Queue using Stacks(用栈实现队列)的更多相关文章

  1. LeetCode OJ:Implement Queue using Stacks(栈实现队列)

    比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...

  2. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. 232 Implement Queue using Stacks 用栈来实现队列

    使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...

  5. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  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 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  8. LeetCode232 Implement Queue using Stacks Java 题解

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

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

  10. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

随机推荐

  1. 分享一个单例模型类Singleton代码

    相关代码: ;                foreach (string key in dict.Keys)                {                    if (cou ...

  2. salesforce零基础学习(八十三)analytics:reportChart实现Dashboard(仪表盘)功能效果

    项目中经常会用到Report以及Dashboard来分析汇总数据,Dashboard可以指定view as user,如果针对不同的用户需要显示其允许查看的数据,比如  根据role hierarch ...

  3. 教你如何实现微信小程序与.net core应用服务端的无状态身份验证

    随着.net core2的发布,越来越多人使用.net core2开发各种应用服务端,下面我就结合自己最近开发的一款小程序,给大家分享下,怎么使用小程序登录后,小程序与服务端交互的权限控制. .net ...

  4. javascript获取链接参数

    var url = "http://test.cn/index.php?class=9&id=2&key=88"; function parseQueryStrin ...

  5. 这应该是目前最快速有效的ASP.NET Core学习方式(视频)

    ASP.NET Core都2.0了,它的普及还是不太好.作为一个.NET的老司机,我觉得.NET Core给我带来了很多的乐趣.Linux, Docker, CloudNative,MicroServ ...

  6. C语言系列之printf和%12d的用法(三)

    看C语言程序的时候,往往会遇到printf函数输出,在此,我想总结一下printf的一般用法以及%12d是什么意思 printf函数的一般格式为 printf(格式控制,输出列表): 例如: prin ...

  7. javaweb-3-在Eclipse中引入Tomcat

    一.在Eclipse中引入Tomcat 第一步: 第二步: 第三步: 第四部:

  8. MyBatis开发学习记录

    使用MyBatis时主要是完成POJO和SQL的映射规则 MyBatis基本构成: SqlSessionFactoryBuilder SqlSessionFactory SqlSession SqlM ...

  9. ASP.NET MVC5+EF6+EasyUI 后台管理系统(88)-Excel导入和导出-自定义表模导出

    前言 之前说了导入和导出,也提供了自定义的表模的导入,可见LinqToExcel可以做的事情不仅仅如此 这次我们来演示比较复杂的导出Excel,导出复杂的Excel与导入复杂的Excel原理基本是一样 ...

  10. .net 分布式的未来:微服务

    一.背景&问题 之前框架是一个基于SOA思想设计的分布式框架.各应用通过服务方式提供使用,服务之间通信是RPC方式调用,具体实现基于.NET的WCF通信平台.框架存在如下2个问题: 1.高并发 ...