方法实现:

//1.3.8
package com.qiusongde; import java.util.Iterator;
import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class ResizingArrayStack<Item> implements Iterable<Item> { private Item[] content;
private int number; public ResizingArrayStack() {
content = (Item[]) new Object[1];
number = 0;
} private void resizeArray(int max) { if(max < number)
throw new IllegalArgumentException("the size of new array must larger than the size of Stack"); Item[] temp = (Item[]) new Object[max];
for(int i = 0; i < number; i++) {
temp[i] = content[i];
}
content = temp;
} public boolean isEmpty() {
return number == 0;
} public int size() {
return number;
} public void push(Item item) { if(number == content.length)
resizeArray(2 * content.length); content[number++] = item;
} public Item pop() { if(isEmpty())
throw new NoSuchElementException("Stack is empty"); Item item = content[--number];
content[number] = null;//Aoid loitering if(number == content.length/4 && number > 0)
resizeArray(content.length/2); return item;
} @Override
public Iterator<Item> iterator() {
return new ReverseArrayIterator();
} private class ReverseArrayIterator implements Iterator<Item> { private int i = number; @Override
public boolean hasNext() {
return i > 0;
} @Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException("Stack is empty");
return content[--i];
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } //Just for test(main)
private int arrayLength() {
return content.length;
} public static void main(String[] args) { ResizingArrayStack<String> stack = new ResizingArrayStack<String>();
StdOut.println("Initialized size:" + stack.size() + " Array Size:" + stack.arrayLength()); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { stack.push(item);
StdOut.println("push success:" + item + " size:" + stack.size() + " Array Size:" + stack.arrayLength()); StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println(); } else {
if(stack.isEmpty())
StdOut.println("pop error, stack empty");
else {
StdOut.println("pop success:" + stack.pop() + " size:" + stack.size() + " Array Size:" + stack.arrayLength()); StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println();
}
} } } }

测试结果:

Initialized size:0 Array Size:1
it
push success:it size:1 Array Size:1
Left on stack: it
was
push success:was size:2 Array Size:2
Left on stack: was it
-
pop success:was size:1 Array Size:2
Left on stack: it
the
push success:the size:2 Array Size:2
Left on stack: the it
best
push success:best size:3 Array Size:4
Left on stack: best the it
-
pop success:best size:2 Array Size:4
Left on stack: the it
of
push success:of size:3 Array Size:4
Left on stack: of the it
times
push success:times size:4 Array Size:4
Left on stack: times of the it
-
pop success:times size:3 Array Size:4
Left on stack: of the it
-
pop success:of size:2 Array Size:4
Left on stack: the it
-
pop success:the size:1 Array Size:2
Left on stack: it
it
push success:it size:2 Array Size:2
Left on stack: it it
was
push success:was size:3 Array Size:4
Left on stack: was it it
-
pop success:was size:2 Array Size:4
Left on stack: it it
the
push success:the size:3 Array Size:4
Left on stack: the it it
-
pop success:the size:2 Array Size:4
Left on stack: it it
-
pop success:it size:1 Array Size:2
Left on stack: it

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

  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. 如何将数据导入到hive中

    可以通过多种方式将数据导入hive表 1.通过外部表导入 用户在hive上建external表,建表的同时指定hdfs路径,在数据拷贝到指定hdfs路径的同时,也同时完成数据插入external表. ...

  2. HDU 小明系列故事——师兄帮帮忙 高速幂

    小明系列故事--师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  3. 【Android】getActionBar()为null的解决方法总结

    前言 在使用 ActionBar的时候,有时候会爆出空指针异常,这是由于应用没有获取到 ActionBar 导致的,而导致应用没有获取到 ActionBar 的原因比較多.所以我们以下就来总结一下 A ...

  4. USB协议[转]__总结得很好

    一 枚举过程:◆ 用户将一个USB设备插入USB端口,主机为端口供电,设备此时处于上电状态.◆主机检测设备.◆集线器使用中断通道将事件报告给主机.◆主机发送Get_Port_Status(读端口状态) ...

  5. 添加w3c验证图片到网站

    1.在http://validator.w3.org/网站验证 2.添加验证通过后的代码至自己的网站 类似<p>    <a href="http://validator. ...

  6. 织梦在广告(myad)中使用css样式

    使用单引号,以及只有style这一个属性

  7. MAC平台create-react-app使用问题(command not found)

    You are able to apply the following solution: $ npm config set prefix /usr/local $ sudo npm install ...

  8. git系列1

    git clone支持多种协议,除了HTTP(s)以外,还支持SSH.Git.本地文件协议等,下面是一些例子. $ git clone http[s]://example.com/path/to/re ...

  9. 图像处理之基础---二维卷积c实现

    http://wenku.baidu.com/link?url=4RzdmvP9sdaaUbnVEW4OyBD-g67wIOiJjKFF3Le_bu7hIiBS7I6hMcDmCXrQwsHvrsPv ...

  10. UVALive 6530 Football (水

    题目链接:点击打开链 #include <cstdio> #include <vector> #include <algorithm> using namespac ...