算法(Algorithms)第4版 练习 1.3.32
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.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- performSelector 方法的自己主动俘获特性
局部变量自己主动俘获 偶然在调试中发现,performSelector 方法具有自己主动俘获变量的特性.试看例如以下代码: CGFloat c = _addViewShowing ? 0 : 80; ...
- Python基础之迭代器
迭代器的优点: 1.可以使用for循环遍历: 2.可以节省内存空间: 3.可以有序的访问集合(set)数据结构内的元素. 迭代器的缺点: 只能向前,不能后退. 可迭代对象与不可迭代对象: 可迭代对象: ...
- Maximum sum-动态规划
A - Maximum sum Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- PHP高级工程师的要求
PHP 高级工程师1名,(3年以上工作经验 ) 1.熟悉unix环境编程,如多线程/多进程,IO复用.锁.定时器.新号.信号量.共享内存.消息队列.文件系统2.熟悉php的stream.sock ...
- python在windows系统中打印中文乱码
转自:http://www.111cn.net/phper/python/58920.htm 中文乱码对于程序开发人员来讲不是什么怪事情了,今天我在使用python打印中文时就出现乱码了,下面我们一起 ...
- Android SDK环境搭建
方法有二 方法一: Android SDK开发包国内下载地址 http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform ...
- Python中使用 Selenium 实现网页截图实例
Selenium 是一个可以让浏览器自动化地执行一系列任务的工具,常用于自动化测试.不过,也可以用来给网页截图.目前,它支持 Java.C#.Ruby 以及 Python 四种客户端语言.如果你使用 ...
- pthon 基础 9.8 增加数据
#/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/24 2:59 #@Auther :liuzhenchuan #@File :增加 ...
- 论JavaWeb前后端分离放弃jsp
1.静态资源使用Nginx反向代理Tomcat,Tomcat挂了网站仍可访问.2.静态与后端服务器分离,提升性能.3.大并发情况下,可同时扩展前后端服务器.4.接口可复用至App相关服务.5.网站热部 ...
- hdu 4414 Finding crosses【简单模拟】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...