下压(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. 在css中设置图片的背景图,怎么设置图片纵向拉伸

    css中设置背景图拉伸填充,在css2.1之前这个背景的长宽值是不能被修改的. 实际的结果是只能重复显示,可以使用repeat,repeat-x,repeat-y,no-repeat这些属性来控制背景 ...

  2. Delphi New,Getmem,ReallocMem联系与区别

    来自:http://www.cnblogs.com/jsrgren/archive/2011/10/31/2270353.html ---------------------------------- ...

  3. k8s的故障切换(failover)

    当前3个节点的状态都为ready 当前node1有两个pod  node2有1个pod 现在将node1关机会有怎样的现象 ping 分布在node1节点的pod地址已经ping不通. 在node1节 ...

  4. 微信小程序保存图片的方法

    1.xhtml代码 长按保存: <view class="img" catchlongpress='baocun'></view> 2.Js代码 baocu ...

  5. centeros7的redis-cli命令不生效解决方法(亲测)

    如果你已经安装了redis服务器,并且已经启动,但是redis-cli命令无法生效,分析,命令未加入环境变量.那就给redis命令加入环境变量中: 注意点:redis安装目录会有不同,注意下面的PAT ...

  6. PHP实现innodb的数据回滚

    //实例化的数据库对象$model = new Model();//开启事务$model->startTrans();//默认结果$judge = true;//数据操作$sql1 =" ...

  7. 【互动问答分享】第13期决胜云计算大数据时代Spark亚太研究院公益大讲堂

    “决胜云计算大数据时代” Spark亚太研究院100期公益大讲堂 [第13期互动问答分享] Q1:tachyon+spark框架现在有很多大公司在使用吧? Yahoo!已经在长期大规模使用: 国内也有 ...

  8. Codeforces Round #424 B. Keyboard Layouts(字符串,匹配,map)

    #include <stdio.h> #include <string.h> ][]; ]; ]; int main(){ scanf(]); scanf(]); scanf( ...

  9. HDU 2710 Max Factor(数论,素数筛法)

    #include<iostream> #include<stdio.h> #include<string.h> #include<cmath> usin ...

  10. codevs 方格取数

    1043 方格取数 2000年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Descri ...