//数组模拟栈 public class ArrayStack { private int maxStacksize; private int[] stack; private int top = -1; public ArrayStack(int maxStacksize){ this.maxStacksize = maxStacksize; stack = new int[maxStacksize];//必须数组初始化 } //判断是否为满栈 public boolean isFull(){ return this.top == this.maxStacksize-1; } //判断是否为空栈 public boolean isEmpty(){ return this.top == -1; } //压栈 public void push(int val){ if(isFull()){ throw new RuntimeException("栈已满"); } top++; stack[top] = val; } //出栈 public int pop(){ if(isEmpty()){ throw new RuntimeException("栈为空"); } int value; value = stack[top]; top--; return value; } //栈中数据查看方法 public void List(){ for(int i=0 ; i<stack.length ; i++){ System.out.println("栈中的数据为:"+ stack[i]+" "); } } //栈中数据的长度 public int lenth(){ return top + 1; } }
Java数据结构之栈的数组实现
最新推荐文章于 2023-03-22 20:39:17 发布