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. 串口接收模块(verilog) 波特率115200

    我来分享一下uart协议之接收verilog代码 顶层实例化 `timecale 1ns / 1ps////////////////////////////////////////////////// ...

  2. main之前初始化流程

    main之前初始化流程 本文分别介绍Keil调用的ARMCC以及ARM-NONE-EABI-GCC两个编译器在main之前的操作: Keil MDK启动文件 总结一下MDK的启动流程: 1.系统初始化 ...

  3. J-Link驱动下载和Hex程序下载

    J-LINK驱动下载 1.首先拥有硬件J-Link硬件. 2.安装J-Link驱动程序SEGGER 下载地址如下 https://www.segger.com/downloads/jlink/JLin ...

  4. 最大流——Dinic算法

    前面花了很长时间弄明白了压入-重标记的各种方法,结果号称是O(V3)的算法测demo的时候居然TLE了一个点,看了题解发现所有人都是用Dinic算法写的,但它的复杂度O(V2E)明显高于前者,具体是怎 ...

  5. [转载] Jupiter代码审查工具使用参考

    转载自http://blog.csdn.net/jemlee2002/article/details/5715355 一.       Jupiter 是什么? 这里的 Jupiter 是一个开源的代 ...

  6. Shiro 加密helloWorld

    承接第一章 初解加密 只贴更改的源码,其他看上一篇. ShiroRealm.java package com.lkk.shiro.realms; import org.apache.shiro.aut ...

  7. javascript函数式编程(一)

    一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...

  8. python 常用库整理

    python 常用库整理 GUI 图形界面 Tkinter: Tkinter wxPython:wxPython pyGTK:PyGTK pyQt:pyQt WEB框架 django:django w ...

  9. canvas图表(4) - 散点图

    原文地址:canvas图表(4) - 散点图 今天开始完成散点图,做完这一节,我的canvas图表系列就算是完成了,毕竟平时最频繁用到的就是这几类图表了:柱状,折线,饼图,散点.经过编写canvas图 ...

  10. sockt初级了解 感悟 一起打怪升级偶

    刚接触来谈谈对sockt基础的一点理解,多线性下次再发.也逛了逛博客,有一篇基础讲的停息在这推荐下sockt套接字编程全绍辉 首先贴下代码#服务器 import socket skt=socket.s ...