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. git revert 后悔了 还原修改前的版本 + git 常用命令

    昨天手残 然后在GitHub for windows 上点了revert 然后就给重置了 更手残的是又给同步了 .  但是 GitHub 会保留之前的版本 . 只要删掉本次修改就可. 解决方案:  g ...

  2. linux命令的别名alias,unalias

    1. 别名 linux别名alias的作用: 1. 简化特别长得命令和參数 2. 对一些命令添加默认选项.提高安全性. 2. alias使用 [www@work sh]$ alias lm='ls - ...

  3. oracle字符乱码的解决方法

    原因分析: 客户端字符集就是为了让数据库知道我们传递过去的字符是属于哪种字符集,以便于Oracle在存储字符时进行相应的编码映射(查看客户端字符集通过查找注册表中的NLS_LANG键).在客户端查询数 ...

  4. 让Category支持添加属性与成员变量【转载】

    Category是Objective-C中常用的语法特性,通过它可以很方便的为已有的类来添加函数.但是Category不允许为已有的类添加新的属性或者成员变量.     一种常见的办法是通过runti ...

  5. springboot 中使用AOP

    网上关于AOP的例子好多,各种名词解释也一大堆,反正名词各种晦涩,自己写个最最最简单的例子入门mark一下,以后再深入学习. maven依赖 <dependency> <groupI ...

  6. 【边做项目边学Android】小白会遇到的问题--Appcompat_V7问题

    问题描写叙述: 首先遇到的问题就是adt版本号的选择,sdk版本号的选择: adt按非小白朋友说的选了最新的ADT-22.3.0,同一时候我也把sdk更新到了最新的(嗯.这个要fanqiang,或者找 ...

  7. Markdown GUI编辑器推荐 windows mac

    windows 1. MarkdownPad 如果右边不能预览: LivePreview is not working - it displays an error message stating T ...

  8. COM组件多接口对象模型

    COM组件有两种接口类型,Dual and Custom,如下图所示.本文说的是Custom.所谓多接口COM对象是指此COM对象实现了多于一个的自定义接口,即Custom接口. 接口图如下: 需要注 ...

  9. thinkPHP5.0的学习研究【架构】

    2017年6月19日18:51:53 架构:1.ThinkPHP5.0应用基于MVC(模型-视图-控制器)的方式来组织.2.MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC ...

  10. poj2075

    Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6348   Accepted: 2505 ...