Listing 8: Stack.java — Stack class based on the LinkedList collection

import java.util.*;

class Stack
{
    private LinkedList data;
    
    public Stack()
    {
        data = new LinkedList();
    }
    public void push(Object o)
    {
        data.addFirst(o);
    }
    public Object pop()
        throws NoSuchElementException
    {
        return data.removeFirst();
    }
    public int size()
    {
        return data.size();
    }
    public void clear()
    {
        data.clear();
    }
}
— End of Listing —