算法(第四版)学习笔记之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 ...
随机推荐
- 执行监听器( Execution listener)
相关类: org.activiti.engine.delegate.ExecutionListener org.activiti.engine.delegate.TaskListener org.ac ...
- python基础之程序交互与数据类型
一.程序交互 1.什么是程序交互? name=input('输入用户名:') #无论用户输入何种内容,input都会存成字符串格式 print(name) 2.为啥要有程序交互? 让计算机能够模拟人, ...
- Java处理文件BOM头的方式推荐
背景: java普通的文件读取方式对于bom是无法正常识别的. 使用普通的InputStreamReader,如果采用的编码正确,那么可以获得正确的字符,但bom仍然附带在结果中,很容易导致数据处理出 ...
- Django 在 view 中使用 Paginator分页插件
1.在 view 中使用 Paginator def query(request,sql): # 创建连接 connection = pymysql.connect(**config) try: wi ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 对Array.prototype.slice.call()方法的理解在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢?
1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说,-1 指最后一个元素,-2 指倒数第 ...
- java客户端编辑为win中可执行文件(exe4j)
exe4j 网址: http://www.ej-technologies.com/products/exe4j/overview.html
- 【博弈论】【SG函数】poj2311 Cutting Game
由于异或运算满足结合律,我们把当前状态的SG函数定义为 它所能切割成的所有纸片对的两两异或和之外的最小非负整数. #include<cstdio> #include<set> ...
- python基础-协程函数、递归、模块、包等内容
1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...
- SpringMVC实现操作的第二种方式
一: 运行效果: 点击提交之后显示效果 二: (1).web.xml <?xml version="1.0" encoding="UTF-8"?> ...