题目原文:

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. c++枚举变量初始值

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha enum foo { c = -1, a = 1, b ...

  2. java_servlet执行流程和生命周期

        

  3. IOS: Xcode报 Undecleared selector Warning错误的解决方法

    Undecleared selector  Warning 是编译器报的,特别是升级到IOS7 默认PROJ设定的时候,会出现这种问题,如果从代码上看语法完全没有问题,那么就可以在Xcode里面设置禁 ...

  4. jinkins配置python虚拟环境

    cd /root/.virtualenvs . automationVenv/bin/activate cd /home/wangju/gitProject/automationtest#cd pyt ...

  5. std::vector遍历

    std::vector是我在标准库中实用最频繁的容器.总结一下在遍历和创建vector时需要注意的一些地方. 在不考虑线程安全问题的前提下,在C++11中有五种遍历方式. 方式一 for (size_ ...

  6. CAD在网页中得到批注信息

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. [luogu2591 ZJOI2009] 函数

    传送门 Solution 画图找规律.. Code //By Menteur_Hxy #include <cstdio> #define min(a,b) ((a)>(b)?(b): ...

  8. yum更换国内源及yum下载rpm包

    一.yum更换国内源 运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 1.前往yum文件路径地址 [root@web1 ~]# cd /etc/yum.repos.d/ [root ...

  9. 39.date hitogram基础知识

    主要知识点: date hitogram之统计每月电视销量     上一节讲到histogram,他是以数值进行分组.本节讲到以日期进行bucket分组操作,也就是说把连续的日期变成离散的日期区间,然 ...

  10. C#学习笔记_11_方法的隐藏和重写

    11_方法的隐藏和重写 方法的隐藏 需要使用到关键字:new 方法的重写 虚函数: 使用关键字virtual修饰的函数 虚函数可以被子类隐藏,也可以被子类重写 非虚函数只能被子类隐藏 关键字:over ...