算法(第四版)学习笔记之java实现可以动态调整数组大小的栈
下压(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实现可以动态调整数组大小的栈的更多相关文章
- 算法(第四版)学习笔记之java实现希尔排序
希尔排序思想:使数组中随意间隔为h的元素都是有序的. 希尔排序是插入排序的优化.先对数组局部进行排序,最后再使用插入排序将部分有序的数组排序. 代码例如以下: /** * * @author seab ...
- 算法第四版学习笔记之优先队列--Priority Queues
软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...
- 算法第四版学习笔记之快速排序 QuickSort
软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators
At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 修改ueditor CSS
- selenium 警告框处理 (弹窗处理)
在web应用中常常会遇见很多用JavaScript编写的alert .confirm 以及prompt 弹窗,这是就需要driver.switchTo().alert()来选取(定位)警告弹窗.再对弹 ...
- thinkphp5最美跳转页面
声明下:此教程来自TP官网,如果需要看原文,请点击一下链接 http://www.thinkphp.cn/code/3437.html 先给大家看下效果: 直接撸代码: 第一步:为了增加对移动设备 ...
- AC日记——病毒侵袭持续中 hdu 3065
3065 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- (十一)数组array
变量:只能存一个值,数组可以存多个值 (1)普通数组,索引下标是整数: 1)定义: 方法一:一次赋一个值:语法:数组名[下标]=变量值 array[1]=linux array[2]=shell 方法 ...
- POJ 1164 城堡问题【DFS/位运算/种子填充法/染色法】
1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | ...
- STL优先队列——踩坑日记
priority_queue 可以定义STL中的优先队列,但是优先队列在应用于自己定义的类型时需要重载<运算符,或者通过仿函数来定义比较方法,在定义比较方法的过程中,比较大的坑是STL中对于参数 ...
- 水流(water)(BFS)(DFS)
水流(water) 时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 2[提交][状态][讨论版] 题目描述 全球气候变暖,小镇A面临水灾,于是你必须买一些泵把水抽走.泵的抽水能力 ...
- 记录(Record)
记录有可以被称为行(Row),可以通俗的认为它是数据表中的一行数据.以员工表为例,一个公司的员工表中的数据是这样的: 这里每一行数据就代表一个员工的资料,这样的一行数据就叫做一条记录.表是由行和列组成 ...
- 2014 非常好用的开源 Android 测试工具
http://www.php100.com/html/it/mobile/2014/1015/7495.html 当前有很大的趋势是转向移动应用平台,Android 是最广泛使用的移动操作系统,201 ...