Stack的实现
public class MyStack<AnyType> {
private AnyType [] theItems;
private final int DEFAULT_CAPACITY = 100;
private int top;
public MyStack(){ //构造函数,生成空表
clear();
}
public void clear(){ //归为默认(清空)
top = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public void ensureCapacity(int newCapacity ){ //重置表的容量
AnyType [] oldItems = theItems;
theItems = (AnyType []) new Object[newCapacity];//****重新分配空间 注意使用强制转换的方式进行定义
for (int i = 0 ; i < top; i++){
theItems[i] = oldItems[i];
}
}
public boolean isEmpty(){ //判断是否为空
return top == 0;
}
public boolean push( AnyType newVal){//末尾添加元素
if(theItems.length == top)
ensureCapacity(top * 2 + 1);
theItems[top++] = newVal;
return true;
}
public AnyType pop(){
if(top == 0)
throw new NoSuchElementException();
return theItems[--top];
}
}
Stack的实现的更多相关文章
- 线性数据结构之栈——Stack
Linear data structures linear structures can be thought of as having two ends, whose items are order ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- Stack操作,栈的操作。
栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Stack的三种含义
作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
随机推荐
- Ubuntu Server 安装部署 Cacti 服务器监控
本文的英文版本链接是 http://xuri.me/2013/10/20/install-the-cacti-server-monitor-on-ubuntu-server.html Cacti是一套 ...
- ckeditor的使用与验证
1.使id=id的textArea变为富文本编辑框 function inittextarea(id) { CKEDITOR.replace(id,{ width:'600px', ...
- Android url中文编码问题
最近项目遇见一个很奇葩问题,关于URL问题,项目中加载图片,图片的URL含有中文,但是,我的手机可以加载,没问题,同事也都可以,但是测试手机却不可以,加载失败,找到问题,就是URL含有中文问题. 解决 ...
- Python的tkinter和tkinter.messagebox应用-鼠标和键盘命令绑定
__author__ = 'Administrator' from tkinter import * import tkinter.messagebox class MainWindow: def b ...
- SQL Server 查看备份集元数据的 4 种方法。
方法 1. restore labelonly 方法 2. restore headeronly 方法 3. restore filelistonly 方法 4. restore verifyonly ...
- MYSQL 时间计算的 3 种函数
方法 1. 加法 adddate('date_expression',interval value type); 'date_expression' + interval value type; -- ...
- What does cmd /C mean? [closed] 关于nodejs的子进程部分
之前一直很不明白为什么 child_process.spawn(command[, args][, options]) shell <Boolean> | <String> I ...
- java面试题大全-基础方面
Java基础方面: 1.作用域public,private,protected,以及不写时的区别答:区别如下:作用域 当前类 同一package 子孙类 ...
- 打开本地STL文件并创建webgl使用的geometry
需求 打开本地STL文件 一个独立基于webgl的viewer,会被别的网站重用 将打开文件的数据传输给viewer,并且在文件加载的时候显示进度条 解决方案 #1可以使用传统的html5 api来打 ...
- :gAudit
http://www.doc88.com/p-0794369847693.html http://baike.baidu.com/link?url=pcOUfBpILuEAPFrBSsSU-6Vzg3 ...