算法(第四版)学习笔记之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 ...
随机推荐
- 运行HelloWorld.class是报错(错误: 找不到或无法加载主类 HelloWorld.class)
1.从毕业到现在工作了几个月了,每天都是在写一些js代码,感觉作为一个web程序员,java还是十分重要的,于是自己买了一本java书来边学边练习,然后发现自己连使用记事本来编写的HelloWorld ...
- Storm中log4j日志打印不出来的解决办法
使用storm命令启动JAVA进程的时候,发现log4j日志打印不出来,咋办呢? 解决办法如下(亲测): 删除strom/lib目录下的log4j-over-slf4j-1.6.6.jar strom ...
- 关于 svn: E155004 is already locked 出现原因和解决办法
1.出错提示: svn: E155004 is already locked,please clean up ......... 2.出错原因: SVN 本地更新时,由于一些操作中断更新,如磁盘空间 ...
- go语言多态接口样例
感觉比java玄幻啊~~~ package main import ( "fmt" ) type notifier interface{ notify() } type user ...
- ubantu16.04安装配置samba服务(原创)
1.安装samba服务 $ sudo apt-get install samba samba-common$ sudo apt-get install smbclient 如果你开启了防火墙,关闭: ...
- AC日记——逆序对 洛谷 P1908
逆序对 思路: 线段树水过: 代码: #include <cstdio> #include <cstring> #include <iostream> #inclu ...
- EF中使用Linq时First、FirstOrDefault、Single、SingleOrDefault几个方法的区别
在使用EntityFramework开发时,.NET的System.Linq.Enumerable类为我们提供了许多Linq方法. 给大家分享一下关于First.FirstOrDefault.Sing ...
- firefox中outlook.com页面卡顿的原因
在火狐中使用outlook.com时,鼠标点击动作后,页面会卡顿一段时间,每次点击都是如此. 因为之前火狐出现由于硬件加速导致页面卡顿的情况,因此第一反应就是关闭硬件加速. 果然,关闭硬件加速后,页面 ...
- [bootstrap] 修改字体
file: variable.less @font-family-sans-serif: "Helvetica Neue", Helvetica, Arial, sans-seri ...
- Lock wait timeout exceeded数据库死锁问题
环境 MySQL5.5 现象 A.数据更新或新增后数据经常自动回滚. B.表操作总报 Lock wait timeout exceeded 并长时间无反应 解决方法 A.应急方法:show proce ...