import java.util.StringTokenizer;
//import java.util.Stack;;

public class List {
    Node head;
    int length;

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

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

    // split: break the string into words and return them in a
    // list of strings.
    public static List split (String str) {
	// push all the tokens onto a stack
	Stack stack = new Stack ();
	StringTokenizer st = new StringTokenizer (str);
	while (st.hasMoreTokens ()) {
	    stack.push (st.nextToken());
	}

	// pop them off the stack and put them in the list
	List list = new List ();
	while (!stack.empty ()) {
	    list.addNewFirstNode (stack.pop ());
	}
	return list;
    }

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

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

    // 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 (")");
    }
}

