ADT:

    /**
* see if Steque is empty
* @return {@code true} Steque is empty
* {@code false} Steque isn't empty
*/
public boolean isEmpty() /**
* return the size of the Steque
* @return return the size of the Steque
*/
public int size() /**
* push one item at the beginning of the steque
*
* @param item the item to be pushed
*/
public void push(Item item) /**
* pop one item at the beginning of the steque
*
* @return return the item at the beginning of the steque
* @throws NoSuchElementException if the steque is empty
*/
public Item pop() /**
* enqueue one item at the end of the steque
* @param item the item to be enqueued
*/
public void enqueue(Item item)

代码实现以及测试:

package com.qiusongde.creative;

import java.util.Iterator;
import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdOut; public class Steque<Item> implements Iterable<Item> { private Node<Item> first;
private Node<Item> last;
private int size; public Steque() {
first = null;
last = null;
size = 0;
} private class Node<E> {
E item;
Node<E> next;
} /**
* see if Steque is empty
* @return {@code true} Steque is empty
* {@code false} Steque isn't empty
*/
public boolean isEmpty() {
return first == null;
} /**
* return the size of the Steque
* @return return the size of the Steque
*/
public int size() {
return size;
} /**
* push one item at the beginning of the steque
*
* @param item the item to be pushed
*/
public void push(Item item) {
Node<Item> oldfirst = first; first = new Node<Item>();
first.item = item;
first.next = oldfirst; if(oldfirst == null) {
last = first;
} size++;
} /**
* pop one item at the beginning of the steque
*
* @return return the item at the beginning of the steque
* @throws NoSuchElementException if the steque is empty
*/
public Item pop() {
if(isEmpty())
throw new NoSuchElementException("Steque is empty"); Item item = first.item;
first = first.next; if(isEmpty())
last = null; size--; return item;
} /**
* enqueue one item at the end of the steque
* @param item the item to be enqueued
*/
public void enqueue(Item item) {
Node<Item> oldlast = last; last = new Node<Item>();
last.item = item;
last.next = null; if(oldlast == null)
first = last;
else
oldlast.next = last; size++;
} @Override
public String toString() {
String s = ""; Node<Item> current = first;
while(current != null) {
s += current.item + " ";
current = current.next;
} s += first == null ? "first:null " : "first:" + first.item + " ";
s += last == null ? "last:null " : "last:" + last.item + " "; return s;
} @Override
public Iterator<Item> iterator() {
return new StequeIterator();
} private class StequeIterator implements Iterator<Item> { private Node<Item> current = first;
@Override
public boolean hasNext() {
return current != null;
} @Override
public Item next() { if(!hasNext())
throw new NoSuchElementException("Steque is empty"); Item item = current.item;
current = current.next; return item;
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } public static void main(String[] args) {
Steque<Integer> steque = new Steque<Integer>();
StdOut.println("Initial steque:");
StdOut.println(steque);
StdOut.println(); //test push function
StdOut.println("Test push function");
for(int i = 1; i <= 10; i++) {
steque.push(i);
StdOut.println("push success: " + i);
StdOut.println(steque);
}
StdOut.println(); //test pop function
StdOut.println("Test pop function");
for(int i = 1; i <= 10; i++) {
int out = steque.pop();
StdOut.println("pop success: " + out);
StdOut.println(steque);
}
StdOut.println(); //test enqueue function
StdOut.println("Test enqueue function");
for(int i = 1; i <= 10; i++) {
steque.enqueue(i);
StdOut.println("enqueue success: " + i);
StdOut.println(steque);
}
StdOut.println(); //end
for(int i : steque) {
StdOut.print(i + " ");
}
StdOut.println(); } }

输出结果:

Initial steque:
first:null last:null Test push function
push success: 1
1 first:1 last:1
push success: 2
2 1 first:2 last:1
push success: 3
3 2 1 first:3 last:1
push success: 4
4 3 2 1 first:4 last:1
push success: 5
5 4 3 2 1 first:5 last:1
push success: 6
6 5 4 3 2 1 first:6 last:1
push success: 7
7 6 5 4 3 2 1 first:7 last:1
push success: 8
8 7 6 5 4 3 2 1 first:8 last:1
push success: 9
9 8 7 6 5 4 3 2 1 first:9 last:1
push success: 10
10 9 8 7 6 5 4 3 2 1 first:10 last:1 Test pop function
pop success: 10
9 8 7 6 5 4 3 2 1 first:9 last:1
pop success: 9
8 7 6 5 4 3 2 1 first:8 last:1
pop success: 8
7 6 5 4 3 2 1 first:7 last:1
pop success: 7
6 5 4 3 2 1 first:6 last:1
pop success: 6
5 4 3 2 1 first:5 last:1
pop success: 5
4 3 2 1 first:4 last:1
pop success: 4
3 2 1 first:3 last:1
pop success: 3
2 1 first:2 last:1
pop success: 2
1 first:1 last:1
pop success: 1
first:null last:null Test enqueue function
enqueue success: 1
1 first:1 last:1
enqueue success: 2
1 2 first:1 last:2
enqueue success: 3
1 2 3 first:1 last:3
enqueue success: 4
1 2 3 4 first:1 last:4
enqueue success: 5
1 2 3 4 5 first:1 last:5
enqueue success: 6
1 2 3 4 5 6 first:1 last:6
enqueue success: 7
1 2 3 4 5 6 7 first:1 last:7
enqueue success: 8
1 2 3 4 5 6 7 8 first:1 last:8
enqueue success: 9
1 2 3 4 5 6 7 8 9 first:1 last:9
enqueue success: 10
1 2 3 4 5 6 7 8 9 10 first:1 last:10 1 2 3 4 5 6 7 8 9 10

算法(Algorithms)第4版 练习 1.3.32的更多相关文章

  1. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. 初识C++之虚函数

    1.什么是虚函数 在基类中用virtual关键字修饰.并在一个或多个派生类中被又一次定义的成员函数.使用方法格式为: virtual 函数返回类型 函数名(參数表) { 函数体 } 虚函数是实现多态性 ...

  2. spring利用ApplicationListener自启动

    近期在用mina获取server的数据,但没有和spring进行集成,就利用ApplicationListener实现了自启动 package com.gamesvr.minaenpo; import ...

  3. Ubuntu14下Hadoop开发&lt;1&gt; 基础环境安装

    准备了一台淘汰的笔记本.单核CPU.3G内存.160G硬盘:准备一个2G的U盘 在官网下载了64位的14.04版本号(麒麟)的ISO.下载UNetbootin(Ubuntu专用U盘安装工具) 使用UN ...

  4. JLink defective

    下载了最新的JLink V622g,打开JLink命令行后,提示以下信息 The connected J-Link is defective,Proper operation cannot be gu ...

  5. UVA 10209

    10209 - Is This Integration ? #include <stdio.h> #include <math.h> /* */ //多次错误都是因为我将PI定 ...

  6. CentOS6.4下编译安装Apache2.4+PHP5.6

    安装Apache2.4: 首先从  http://httpd.apache.org/download.cgi#apache24下载apache源码包httpd-2.4.4.tar.gz从  http: ...

  7. iOS开发之 AES+Base64数据混合加密与解密

    2016-04-08 09:03 编辑: liubinqww 分类:iOS开发 来源:liubinqww 投稿 4 889     "APP的数据安全已经牵动着我们开发者的心,简单的MD5/ ...

  8. Vue实现组件props双向绑定解决方案

    注意: 子组件不能直接修改prop过来的数据,会报错 方案一: 用data对象中创建一个props属性的副本 watch props属性 赋予data副本 来同步组件外对props的修改 watch ...

  9. LINQ TO SQL:操作有层次关系的对象

    对于关系型数据与对象数据之间最大的隔阂就是由标识列连接起来的行(关系型数据)与由集合保存的对象(对象数据)之间的冲突. 例如某个Subject对象(也就是数据库中的Subject表),从Subject ...

  10. Binding基础 文摘

    简要 Binding基础 Binding源与路径 列举Binding的源 Binding基础 从Coding中看Binding的基础. 先定义一个Student类: public class Stud ...