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. J1002.JavaFX简介

    引言 2008年12月05日,SUN发布了JavaFX第一个正式版本,以期望Java在UI端能够更好地应用于开发富客户端的互联网应用(Rich Internet Cliet). 2011年发布的Jav ...

  2. HTTP协议相关知识点

    主要参考 http://www.imooc.com/article/14397,来源:慕课网,作者种子_fe HTTP是超文本传输协议,主要特点有: 支持客户.服务器模式 简单快速:客户向服务器请求服 ...

  3. java_IO流读取本地文件

    package com.ht.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoun ...

  4. Java多线程与并发模型之锁

    这是一篇总结Java多线程开发的长文.文章是从Java创建之初就存在的synchronized关键字引入,对Java多线程和并发模型进行了探讨.希望通过此篇内容的解读能帮助Java开发者更好的理清Ja ...

  5. 使用apache进行域名绑定

    [背景] 项目需要搭建一套mysqlapi的开发环境,进行域名绑定 [方法] 主要方式通过修改apache的vhost配置文件,重启apache服务,以及最终在客户端绑定hosts. 1.查看apac ...

  6. Python之多进程篇

    Process 创建子进程执行指定的函数 >>> from multiprocessing import Process,current_process >>> & ...

  7. List实现

    1.元素添加 #include <stdio.h> #include <stdlib.h> struct ListNode{ struct ListNode* next; in ...

  8. 【Java框架型项目从入门到装逼】第二节 - Spring框架 AOP的丧心病狂解说,你喜欢露娜的月下无限连吗?

    继续上一节的内容,多几个jar包: aop技术是面向切面编程思想,作为OOP的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想.AOP底层也是面向对象:只不过面向的不是普通的O ...

  9. Java中常用加减密方式

    1.加密概述: 加密就是是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使以获得了加密的信息,但因不知解密方式,仍无法了解信息的内容.大体上又分为双向加密和单向加密. 2.单项加密 2.1.概 ...

  10. openstack集群环境准备

    #0.openstack集群环境准备 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #openstack集群环境准 ...