自定义数组实现栈的结构。
1 package test; 2 3 public class MyArrayStackClient { 4 public static void main(String[] args) { 5 ArrayStack<Integer> stack = new ArrayStack<Integer>(); 6 // ArrayStack<Integer> stack=new ArrayStack<Integer>(); 7 // 入栈 8 stack.push(6); 9 stack.push(5); 10 stack.push(4); 11 stack.push(3); 12 stack.push(2); 13 stack.push(1); 14 15 System.out.println("此时栈是否为空:" + stack.isEmpty());// 判断栈是否为空 16 System.out.println("获取栈顶元素: " + stack.peek());// 获取栈顶元素 17 System.out.println("当前栈内元素共有: " + stack.size() + "个"); 18 System.out.println("元素: " + stack.pop() + "出栈");// 元素出栈 19 System.out.println("当前栈内元素共有: " + stack.size() + "个"); 20 stack.clear(); 21 System.out.println("当前栈内元素共有: " + stack.size() + "个"); 22 23 } 24 }
1 package test; 2 3 public class ArrayStack<T> implements IStack<T> { 4 private final int DEFAULT_SIZE = 3; 5 private int size = 0; 6 private int capacity = 0; 7 8 // top指向下一个能够添加元素的位置 9 private int top = 0; 10 private Object[] array; 11 12 public ArrayStack() { 13 this.capacity = this.DEFAULT_SIZE; 14 this.array = new Object[this.capacity];// 初始栈 15 this.size = 0; 16 } 17 18 public ArrayStack(int capacity) { 19 this.capacity = capacity; 20 this.array = new Object[this.capacity]; 21 this.size = 0; 22 } 23 24 // 栈中元素为空 25 @Override 26 public boolean isEmpty() { 27 // TODO Auto-generated method stub 28 return size == 0; 29 } 30 31 // 获取栈顶元素 32 @Override 33 public T peek() { 34 // TODO Auto-generated method stub 35 return (T) this.array[this.top - 1]; 36 } 37 38 @Override 39 public T pop() { 40 // TODO Auto-generated method stub 41 T element = (T) this.array[top - 1]; 42 this.array[top - 1] = null; 43 this.size--; 44 return element; 45 } 46 47 // 元素入栈 48 @Override 49 public void push(T element) { 50 // TODO Auto-generated method stub 51 if (this.size < this.capacity) { 52 this.array[top] = element; 53 this.top++; 54 this.size++; 55 } else { 56 // System.out.println("out of array"); 57 enlarge(); // 扩大栈的容量 58 push(element); 59 } 60 } 61 62 // 扩大栈的容量 63 public void enlarge() { 64 this.capacity = this.capacity + this.DEFAULT_SIZE; 65 Object[] newArray = new Object[this.capacity]; 66 // System.arraycopy(array, 0, newArray, 0, array.length); 67 arrayCopy(array, newArray); 68 fillArray(array, null); // 用null将array填满 69 this.array = newArray; 70 } 71 72 // 复制元素 73 public void arrayCopy(Object[] array, Object[] newArray) { 74 for (int i = 0; i < array.length; i++) { 75 newArray[i] = array[i]; 76 } 77 } 78 79 // 向数组中装填元素 80 public void fillArray(Object[] array, Object val) { 81 for (int i = 0; i < array.length; i++) { 82 array[i] = val; 83 } 84 } 85 86 // 求出此时栈中元素的大小 87 public int size() { 88 return this.size; 89 } 90 91 // 清空栈 92 @Override 93 public void clear() { 94 // TODO Auto-generated method stub 95 fillArray(array, null); 96 this.top = 0; 97 this.size = 0; 98 this.capacity = this.DEFAULT_SIZE; 99 this.array = new Object[this.capacity]; 100 } 101 }
1 package test; 2 3 public interface IStack<T> { 4 // 元素出栈,并返回出栈元素 5 public T pop(); 6 7 // 元素入栈 8 public void push(T element); 9 10 // 获取栈顶元素 11 public T peek(); 12 13 // 判断栈是否为空 14 public boolean isEmpty(); 15 16 // 清栈 17 public void clear(); 18 }
输出结果如下:
1 此时栈是否为空:false 2 获取栈顶元素: 1 3 当前栈内元素共有: 6个 4 元素: 1出栈 5 当前栈内元素共有: 5个 6 当前栈内元素共有: 0个