/* List class example. The Node class defines a node with an id and a next field. The class, ListClass, has methods to add nodes to the front and rear of the list and to display the contents of the list.*/

interface ListInterface
{
     public void addToFront (Node newFront);
     public void addToRear (Node newRear);
     public void displayList ();
} // interface ListInterface

class Node
{
    protected String id;
    private Node next;

    public Node (String id) {this.id = id;}
    public int getId () {return id;}
    protected Node getNext () {return next;}
    protected void setNext (Node nextNode) {next = nextNode;}
} // class Node

public class ListClass implements ListInterface
{
    private Node listHead;

    public ListClass () { listHead = null;  }

    // Method to add a node to the front of the list.
    public void addToFront (Node front)
    {
        if (listHead == null) listHead = newFront;
        else
        {
            newFront.setNext (listHead);
            listHead = newFront;
        }
    } // method addToFront

    // Method to add a node to the end of the list.
    public void addToRear (Node rear)
    {
        if (listHead == null) listHead = newRear;
        else // Find the end of the list.
        {
            Node previous = listHead, current = listHead;
            while (current != null)
            {
                previous = current;
                current = current.getNext ();
            }
            previous.setNext (rear);
        }
    } // method addToRear

    // Method to display the numbers in the list.
    public void displayList ()
    {
        Node tempNode = listHead;
        while (tempNode != null)
        {
            System.out.println ("ID: " + tempNode.getId ());
            tempNode = tempNode.getNext ();
        }
    } // method displayList
} // class ListClass