标签:
package bin; import java.util.ArrayList; import java.util.List; import javax.management.RuntimeErrorException; /** * @author bin 15/3/10 * target 共享栈空间 * */ public class Bin_stack_share { private int top1;//栈一的栈顶记录 private int top2;//栈二的栈顶记录 private Object obj[];//存储容器 private int type; private int size;//栈长度 public int getTop1() { return top1; } public void setTop1(int top1) { this.top1 = top1; } public int getTop2() { return top2; } public void setTop2(int top2) { this.top2 = top2; } public Object[] getObj() { return obj; } public void setObj(Object[] obj) { this.obj = obj; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } //构造方法 初始化栈长度 public Bin_stack_share(int size){ this.setTop1(-1); this.setTop2(size); this.setSize(size); this.setObj(new Object[size]); } //进栈方法 public void push(Object val,int type){ if(top1+1 == top2){ throw new RuntimeException("栈满"); } //如果type 为1 则入栈一 else 为栈二 if(type == 1){ obj[top1+1] = val; top1 = ++top1; }else{ obj[top2-1] = val; top2 = --top2; } } public Object pop(int type){ if(top1 == -1 && top2 == size){ throw new RuntimeException("空栈"); } Object temp = new Object(); if(type == 1){ //当TYPE等于1的时候 我们弹出栈1的栈顶 temp = obj[top1]; obj[top1] = null; top1 = --top1; }else{ temp = obj[top2]; obj[top2] = null; top2 = ++top2; } return temp; } public static void main(String[] args) { Bin_stack_share b = new Bin_stack_share(2); b.pop(2); System.out.println(b.top1); } }
标签:
原文地址:http://www.cnblogs.com/bin-pureLife/p/4326633.html