public class LinkedList {
    Node head;
    int length;

    public LinkedList () {
	head = null;
	length = 0;
    }

    public LinkedList (Node head, int length) {
	this.head = head;
	this.length = length;
    }

    // addFirst: create a new node with the given cargo
    // and add it at the beginning of the list
    public void addFirst (Object cargo) {
	head = new Node (cargo, head);
	length++;
    }

    // removeFirst: remove and return the first node
    // in the list, or null if the list is empty
    public Object removeFirst () {
	Node node = head;
	if (node == null) {
	    return null;
	} else {
	    head = head.next;
	    length--;
	    return node.cargo;
	}
    }

    // print: print the list
    public void print () {
	Node node;

	System.out.print (length + " (");

	// start at the beginning of the list
	node = head;

	// traverse the list, printing each element
	while (node != null) {
	    System.out.print (node);
	    node = node.next;
	    if (node != null) {
		System.out.print (", ");
	    }
	}	
	System.out.println (")");
    }
}

