栈——数组实现
2016-04-13 20:37
387 查看
public class SequeceStack<T> {private static int DEFAULT_SIZE = 16;private Object[] stack;private int n; //栈中实际元素个数private int max_capacity;/*** 以默认值构造栈*/public SequeceStack() {this(DEFAULT_SIZE);this.n=0;}/*** 指定值构造栈* @param args*/public SequeceStack(int capacity) {stack = new Object[capacity];this.n=0;}/*** 获得栈中元素个数* @param args*/public int getNumber() {return this.n;}/*** 判断栈是否为空* @param args*/public boolean isEmpty() {return (n==0? true:false);}/*** 入栈* @param args*/public boolean push(T data) {if(this.n<max_capacity) {System.out.println("栈已满,添加失败!");return false;}this.stack[this.n] = data;this.n++;return true;}/*** 出栈* @param args*/public T pop() {if(this.n<=0) {System.out.println("栈为空,出栈失败!");return null;}this.stack[n-1] = null;this.n--;return (T) this.stack[this.n];}/*** 为链表添加10容量*/public boolean addCapacity() {this.stack = new Object[this.max_capacity + 10];return true;}/*** 清空栈* @param args*/public boolean clear() {for(int i=0; i<this.n; i++) {this.stack[i] = null;}System.gc();return true;}public static void main(String[] args) {SequeceStack<String> ss = new SequeceStack<String>();//System.out.println(ss.isEmpty());for(int i=1; i<=10; i++) {ss.push(i + "");}ss.pop();for(int i=0; i<10; i++)System.out.println(ss.stack[i]);}}
相关文章推荐
- STM32F10x命名规则
- ddd
- LeetCode31:Next Permutation
- clicaptcha中文点击验证码开发经验总结
- 17.KVM安装之虚拟磁盘,安装脚本
- 文本和字体
- NoSql数据库使用心得
- ZOJ 3715 投票选国王 (枚举+贪心+模拟)
- 从一个点云里面创建一个深度图
- LeetCode笔记:171. Excel Sheet Column Number
- Delphi 10.1 柏林更新内容简译
- CSS:CSS 在工程中改变——面向对象的CSS (OO CSS)
- ios CALayer的Frame,bounds,position,anchorPoint
- ubuntu mysql表名大小写区分
- ubuntu mysql表名大小写区分
- 故障案例,db启动失败,报错mysqld: File binlog not found (Errcode: 2)
- UML建模—EA的使用(一)EA基本设置及类的设计
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\helloiis\ceb8cab3\4db603d8\App_global.asax.gr73hi-k.dll”--“拒绝访问。 ”
- Android用Gson解析复杂Json
- iOS开发Objective-C编码规范