题目原文:

Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.

题目要求用栈实现队列的所有操作。

 package week2;

 import java.util.Stack;

 /**
* Queue with two stacks. Implement a queue with two stacks so that each queue
* operations takes a constant amortized number of stack operations
* @author yangjingjing
*
*/
public class QueueWith2Stacks<E>{
Stack<E> inStack = new Stack<E>();
Stack<E> outStack = new Stack<E>();
public boolean isEmpty(){
if(inStack.isEmpty() && outStack.isEmpty())
return true;
else return false;
}
public int size(){
return (inStack.size() + outStack.size());
}
public void enqueue(E item){
inStack.push(item);
}
public E dequeue(){
if(outStack.isEmpty()){
if(inStack.isEmpty()) return null;
else{
while(!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
return outStack.pop();
}
}else{
return outStack.pop();
}
}
public static void main(String[] args) {
QueueWith2Stacks<Object> queue = new QueueWith2Stacks<Object>();
queue.enqueue("a");
queue.enqueue("b");
queue.enqueue("c");
System.out.println(queue.dequeue());
queue.enqueue(1);
queue.enqueue(2);
Object o = null;
while((o = queue.dequeue()) != null) {
System.out.println(o);
}
}
}

Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks的更多相关文章

  1. Coursera Algorithms week2 栈和队列 练习测验: Stack with max

    题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push a ...

  2. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)

    作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...

  3. LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

    题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...

  4. C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...

  5. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  6. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  7. Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets

    题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...

  8. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

随机推荐

  1. js 学习笔记---BOM

    window对象 1. window 对象是Global对象,在全局作用域中声明的变量和函数都可以通过window.来访问.跟直接在window上添加属性效果一样.唯一的区别就是delete时,如果是 ...

  2. Lazarus 1.6 增加了新的窗体编辑器——Sparta_DockedFormEditor.ipk

    一下是该控件官网的介绍 "Hello A package for a docked form editor can be found in : components/sparta/docke ...

  3. 脚本添加删除nginx配置中的内容

    [root@nodejs script]# more editnginx.sh #!/bin/bash # function back_check(){ # 备份配置和覆盖配置文件 cp -rf /e ...

  4. JS DOM节点(当前标签和同级、父级、子级..之间的关系)

    1. 通过顶层document节点获取    1) document.getElementById(elementId) //根据id获得    2) document.getElementsByNa ...

  5. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  6. 环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)

    长度为n的环状串有n种表示法,分别为从某 个位置开始顺时针得到.例如,图3-4的环状串 有10种表示: CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等. 在这些表示法中,字典序最小 ...

  7. Charles 下载-破解-安装-配置

    我当前使用版本为V4.2.7 最新版本下载地址 Charles 在线破解工具 下载完之后,先进行安装,安装完之后,根据破解链接中的步骤来就ok了. 比较费劲配置在下面,不过跟着一步步来就一定能好的 点 ...

  8. Python类中的 私有变量和私有方法

    默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类似public,private等关键词来修饰成员函数和成员变量.在python中定义私有变量只需要在变量 ...

  9. 远程连接工具putty与mtputty

    PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件 官网 http://www.chiark.greenend.org.uk/~sgtatham/putty/ putty ...

  10. Spring+SpringMVC+MyBatis整合教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...