ITeye博客

栈 java实现

拳拳天下 · 2015-01-29 17:12 · 数据结构 · 编程语言 · Java · Markdown

package com.shine.stack; /* * 栈的接口 */ public interface SStack { boolean isEmpty(); void push(T x); T pop(); T get(); } package com.shine.stack; /* * 顺序栈类 */ public class SeqStack implements SStack { private Object element[]; private int top; public SeqStack(int size){//构造容量为size的空栈 this.element = new Object[Math.abs(size)]; this.top = -1; } public SeqStack(){ this(64); } @Override public boolean isEmpty() { return this.top==-1; } @Override public void push(T x) {//入栈操作 if(x==null) return; if(this.top==this.element.length-1){//若栈满,需要扩充栈的容量 Object[] temp = this.element; this.element = new Object[temp.length*2]; for(int i=0;i implements SStack{ private Node top; public LinkedStack(){ this.top = null; } @Override public boolean isEmpty() { return this.top==null; } @Override public void push(T x) { if(x!=null) this.top = new Node(x,this.top); } @Override public T pop() { if(this.top == null) return null; T temp = this.top.data; this.top = this.top.next; return temp; } @Override public T get() { return this.top==null?null:this.top.data; } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。