赞
踩
public class ArrayStack { private int maxSize; private int top; private int[] arr; // 约定栈最大深度 public ArrayStack(int maxSize) { this.maxSize = maxSize; arr = new int[maxSize]; } // 判断是否已满 public boolean isFull() { return maxSize <= top; } // 判断是否为空 public boolean isEmpty() { return top <= 0; } // 入栈 public void push(int n) { if (isFull()) throw new RuntimeException("man"); arr[top] = n; ++top; } // 出站 public int pop() { if (isEmpty()) throw new RuntimeException("kong"); return arr[--top]; } // 遍历栈 public void list() { try { for (; ; ) { System.out.println(pop()); } } catch (RuntimeException r) { } } }
加油噢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。