栈最大的一个特点就是先进后出(FILO—First-In/Last-Out)。
/** * 栈:后进先出 * Created by fred on 2018/7/31. */ public class MyStack { private int[] elements; private int size; private final int DEFAULT_CAPACITY = 2; public MyStack(){ elements = new int[DEFAULT_CAPACITY]; } //判断是否为空 public boolean isEmpty(){ return size == 0; } //获取栈的长度 public int getSize(){ return size; } //数据入栈 public void push(int value){ if(size >= elements.length){ int[] tmp = new int[elements.length * 2]; System.arraycopy(elements, 0, tmp, 0, elements.length); elements = tmp; } elements[size++] = value; } //出栈 public int pop(){ if(isEmpty()){ System.out.println("栈为空"); return -1; } else { return elements[--size]; } } }