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 ...
随机推荐
- CodeForces 501B - Misha and Changing Handles
有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...
- Java三大特征之继承(二)
在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...
- has leaked ServiceConnection com.baidu.location.LocationClient
02-06 05:01:52.806: E/ActivityThread(1120): Activity com.project.xxxActivity $1@45fc5af8 that was or ...
- css 兼容小三角
<!DOCTYPE><html ><head><meta http-equiv="Content-Type" content=" ...
- HTTP based RESTful APIs - asp.net web api
1.HTTP http://www.w3.org/Protocols/rfc2616/rfc2616.html 2.REST是什么http://www.ics.uci.edu/~fielding/pu ...
- ios控制器modal跳转
1. http://www.cnblogs.com/smileEvday/archive/2012/05/29/presentModalViewController.html 2012年5月- Pre ...
- Java中的import
有些人写了一阵子 Java,可是对于 Java的 package 跟 import 还是不太了解.很多人以为原始码 .java 文件中的 import 会让编译器把所 import 的程序通通写到编译 ...
- 【OpenCV新手教程之十一】 形态学图像处理(二):开运算、闭运算、形态学梯度、顶帽、黑帽合辑
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/23184547 作者:毛星云(浅墨) ...
- UILabel,UITextField 以及UIButton应用
</pre><pre name="code" class="cpp">一.UILabel 它是ioS开发使用的控件来显示文本,它是UIV ...
- android xUtils的使用
gethub地址:https://github.com/wyouflf/xUtils/ xUtils简介 xUtils 包含了很多实用的android工具. xUtils 支持大文件上传,更全面的ht ...