下压(LIFO)栈:可以动态调整数组大小的实现

import java.util.Iterator;

public class ResizingArrayStack<Item> implements Iterable<Item>
{ private int N = 0;
private Item[] a = (Item[]) new Object[1]; public boolean isEmpty()
{
return N == 0;
}
public int size()
{
return N;
}
public void resize(int max)
{
Item[] temp = (Item[]) new Object[max];
for(int i = 0 ; i < N ; i++)
{
temp[i] = a[i];
}
a = temp;
} public Item pop()
{
Item item = a[--N];
a[N] = null;
if(N > 0 && N == a.length / 4)
{
resize(a.length / 2);
}
return item;
} public void push(Item item)
{
if(N == a.length)
{
resize(a.length * 2);
}
a[N++] = item;
} @Override
public Iterator<Item> iterator() {
// TODO Auto-generated method stub
return new reverseArrayIterator();
} private class reverseArrayIterator implements Iterator<Item>
{ private int i = N;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return i > 0;
} @Override
public Item next() {
// TODO Auto-generated method stub
return a[--i];
} @Override
public void remove() {
// TODO Auto-generated method stub } } }

长处: 

差点儿(但还没用)达到了随意集合类数据类型的实现的最佳性能: 

1.每项操作的用时都与集合大小无关; 

2.空间需求总是不超过集合大小乘以一个常数。

缺点: 

某些push()和pop()操作会调整数组的大小,这项操作的耗时和栈的大小成正比。

算法(第四版)学习笔记之java实现可以动态调整数组大小的栈的更多相关文章

  1. 算法(第四版)学习笔记之java实现希尔排序

    希尔排序思想:使数组中随意间隔为h的元素都是有序的. 希尔排序是插入排序的优化.先对数组局部进行排序,最后再使用插入排序将部分有序的数组排序. 代码例如以下: /** * * @author seab ...

  2. 算法第四版学习笔记之优先队列--Priority Queues

    软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...

  3. 算法第四版学习笔记之快速排序 QuickSort

    软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  5. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  7. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

    Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

    To solve the general programming problem, you need to create any number of objects, anytime, anywher ...

随机推荐

  1. KDJ回测

    # -*- coding: utf-8 -*- import os import pandas as pd # ========== 遍历数据文件夹中所有股票文件的文件名,得到股票代码列表stock_ ...

  2. Bringing up interface eth0: Determining if ip address 10.109.67.81 is already in use for device eth0...

    重启网卡出现提示: Bringing up interface eth0:  Determining if ip address 10.109.67.81 is already in use for ...

  3. IE8的 JS 引擎如此不堪(二) - 解决方案

    上一篇,IE8的JS引擎内存不停增长,是因为动态生成了一个image对象导致的.有了病因,就开始寻找治疗方法. 1.使用一个固定的img对象,但是无法获取地址改变后的图片大小,最总还是放弃: 2.寻找 ...

  4. GridView的TemplateField

    BoundField只能显示一个单独的数据字段.如果我们想要在一个GridView列中显示两个或者更多的数据字段的值的时候该怎么办呢? 1. GridView的一列同时显示数据源中的两个字段 现需要显 ...

  5. HDU 1711 Number Sequence【kmp求子串首次出现的位置】

      Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...

  6. hdu6138(后缀数组)

    hdu6138 题意 给出若干个字符串,每次查询两个字符串,求两个字符串的公共子串且在给出的某一个字符串中作为前缀的最大长度. 分析 求公共子串:后缀数组 判断前缀:字典树 求完后缀数组,遍历下 \( ...

  7. [转载]数学【p1900】 自我数

    题目描述-->p1900 自我数 本文转自@keambar 转载已经原作者同意 分析: 思路还是比较好给出的: 用类似筛选素数的方法筛选自我数. 但是要注意到题目限制的空间仅有4M,不够开10^ ...

  8. boost::operators

    boost 的 operators 提供了comparison operators.arithmetic operators.operators for iterators 操作.虽然使用 C++ 的 ...

  9. 14、Flask实战第14天:Flask使用SQLAlchemy

    flask-sqlalchemy使用详解 之前我们用到的SQLAchemy是可以单独使用的,不需要用到Flask 如果我们在Flask框架中使用SQLAchemy,可以使用flask_sqlalche ...

  10. volatile 和 Interlocked

    class Volatile_Test3 { ; public static void Test() { count = ; Task[] tasks = ]; ; i < tasks.Leng ...