算法(Algorithms)第4版 练习 1.3.8
方法实现:
//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.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 ...
随机推荐
- linux中shell script的追踪与调试
Shell调试篇 sh [-nvx] scripts.sh -n:不要执行script,仅查询语法的问题: -v:在执行script前,先将script的内容输出到屏幕上: -x:将使用到的scrip ...
- Eclipse Memory Analyzer安装
转载:http://www.jianshu.com/p/3b3c3a914724 1.下载地址:Eclipse Memory Analyzer Open Source Project 2.点击进入 ...
- js:"use strict"; 严格模式
http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html
- jQuery与ajax的应用(一)
<body> <div id="resText"></div> <div id="reshtml"></d ...
- VueJS自定义过滤器:new Vue({filters:{filter1:function(){}....}})
Vue.js 允许你自定义过滤器,被用作一些常见的文本格式化. 语法 <!-- 在两个大括号中 --> {{ message | capitalize }} <!-- 在 v-bin ...
- Objective-C学习笔记(十九)——对象方法和类方法的相互调用
事实上在OC的对象方法(减号方法)和类方法(加号方法)并非相互独立的,它们也能够发生千丝万缕的关系,今天我们来研究下它们两者相互调用的问题.该样例还是以People类为基础. (一)对象方法调用类方法 ...
- Redis(十):使用RedisTemplate执行Redis脚本
对于Redis脚本使用过的同学都知道,这个主要是为了防止竞态条件而用的.因为脚本是顺序执行的.(不用担心效率问题)比如我在工作用,用来设置考试最高分. 如果还没有用过的话,先去看Redis脚本的介绍, ...
- python sax解析xml
#books.xml<catalog> <book isbn="0-596-00128-2"> <title>Python & XML& ...
- Android中BaseAdapter使用基础点
Android中要填充一些控件(如ListView)经常须要用到Adapter来实现,经常使用的有ArrayAdapter,SimpleAdapter, CursorAdapter,BaseAdapt ...
- 如何将linux服务器作为文件服务器
在开发过程中想要使用linux服务器作为文件服务器,可以通过 IP+文件名来获取文件信息,比如http://localhost/banner/a.jpg.设置过程如下 1.安装apache2 sudo ...