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. angularjs中的路由介绍详解 ui-route

    这篇文章主要介绍了Angularjs中UI Router全攻略,涉及到angularjs ui router的基本用法,需要的朋友参考下吧   首先给大家介绍angular-ui-router的基本用 ...

  2. 钩子编程(HOOK) 安装进程内键盘钩子 (1)

    摘要:钩子能够监视系统或进程中的各种事件消息.截获发往目标窗体的消息并进行处理.这样,我们就能够在系统中安装自己定义的钩子,监视系统中特定事件的发生.完毕特定的功能,比方截获键盘.鼠标的输入.屏幕取词 ...

  3. 如何突破PHP程序员的技术瓶颈分析

    来自:http://www.jb51.net/article/27740.htm 身边有几个做PHP开发的朋友,也接触到不少的PHP工程师,他们常疑虑自己将来在技术上的成长与发展,我常给他们一些建议, ...

  4. iOS 字符属性NSAttributedString描述【转载】

    /* 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFontAttributeName;(字体) NSString *const N ...

  5. [译]GLUT教程 - 整合代码6

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI 下面代码以窗体模式启动.你可以在 ...

  6. 记pytorch版faster rcnn配置运行中的一些坑

    记pytorch版faster rcnn配置运行中的一些坑 项目地址 https://github.com/jwyang/faster-rcnn.pytorch 一般安装配置参考README.md文件 ...

  7. Java多线程之~~~~synchronized 方法

    在多线程开发中,总会遇到多个在不同线程中的方法操作同一个数据,这样在不同线程中操作这个数据不同的顺序 或者时机会导致各种不同的现象发生,以至于不能实现你预期的效果,不能实现一致性,这时候就能够使用 s ...

  8. lua元表(简单例子)

    Set = {} Set.mt = {}--定义普通的表作为元表,为了避免命名污染直接放在Set内部 function Set.new(t) local set = {} setmetatable(s ...

  9. openCV图像形态学

    #include <cv.h> #include <highgui.h> #include <stdio.h> //平滑处理 int main() { IplIma ...

  10. hdu 2108 Shape of HDU【判断多边形是否是凸多边形模板】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2108 http://acm.hust.edu.cn/vjudge/contest/view.action ...