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. SpringSecurity学习三----------通过Security标签库简单显示视图

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  2. Maven的配置地址

    http://howtodoinjava.com/tools/eclipse/how-to-import-maven-remote-archetype-catalogs-in-eclipse/ htt ...

  3. ffmpeg截图

    ffmpeg.exe -probesize 32768 -i "rtmp://localhost/live/1 live=1" -y -t 0.001 -ss 1 -f image ...

  4. 大型网站技术学习-2. 云计算之OpenStack简述

    上一章讲,虚拟化能够充分的利用资源,带来各种各样的好处. 当一个网站不大,只需要四五台机器就可以支撑的时候,可以采用手工的方式虚拟机,但是当网站流量很高,需要成千上万台机器的时候,那就非常不方便了. ...

  5. PHP代码中使用post参数上传大文件

    今天连续碰到了两个同事向我反应上传大文件(8M)失败的事情! 都是在PHP代码中通常使用post参数进行上传文件时,当文件的大小大于8M时,上传不能不成功. 首先,我想到了nginx的client_m ...

  6. HTTP Status Codes 状态码

    Network Connect Timeout Error

  7. centos7 修改sudoers文件

    使用root账户用 visudo 命令来修改. 转自: https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers ...

  8. 【BZOJ3640】JC的小苹果 概率DP+高斯消元

    [BZOJ3640]JC的小苹果 Description 让我们继续JC和DZY的故事. “你是我的小丫小苹果,怎么爱你都不嫌多!” “点亮我生命的火,火火火火火!” 话说JC历经艰辛来到了城市B,但 ...

  9. CMD命令获取电脑所有连接过的WiFi密码

    cmd中输入命令:for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo ...

  10. WndProc漏写override会发生什么情况?

    试图改写TForm1(注意,不是TForm类)的WndProc函数,从而达到某些目的.程序如下: unit Unit1; interface uses Windows, Messages, SysUt ...