1. 程式人生 > >棧的動態陣列實現

棧的動態陣列實現

棧的特性:先進後出

public class Stack<E> {

    private ArrayList<E> list = new ArrayList<E>();

    //1、置空棧 
    public void empty()
    {
        list.empty();
    }
    //2、判棧空 
    public boolean isEmpty()
    {
        return list.isEmpty();
    }

    //3、入棧  
    public void push(E element)
    {
        list.add(element);
    }
    //4、出棧  
public E pop() { E element = null; if(isEmpty()) { throw new RuntimeException("棧為空"); } //獲取 element = list.get(list.size()-1); //刪除元素 list.sub(list.size()-1); return element; } //5、取棧頂 public E top
() { E element = null; if(isEmpty()) { throw new RuntimeException("棧為空"); } //獲取 element = list.get(list.size()-1); return element; } }