数组实现栈

		public class ArrayStark {
		    private int maxSize;//栈容量
		    private int top = -1;//表示栈顶
		    private int[]stack;
		
		    public ArrayStark(int maxSize) {
		        this.maxSize = maxSize;
		        stack = new int[maxSize];
		    }
		
		    //判空
		    public boolean isEmpty(){
		        return top==-1;
		    }
		
		    //判满
		    public boolean isFull(){
		       if(top==maxSize-1)return true;
		       else return false;
		    }
		
		    //弹栈
		    public int pop(){
		        if(isEmpty())throw new RuntimeException("栈空");
		        int sum = stack[top];
		        top--;
		        return sum;
		    }
		
		    //压栈
		    public void push(int sum){
		        if(isFull()){
		            System.out.println("栈满");
		            return;
		        }
		        stack[++top] = sum;
		    }
		}
posted @ 2020-05-11 21:31  爱敲代码的小游子  阅读(2)  评论(0)    收藏  举报