//ClassWithNestedNode.java

public class ClassWithNestedNode
{
    Node head;

    public static void main(String[] args)
    {
        ClassWithNestedNode tester = new ClassWithNestedNode();
        tester.createAndModifySequence();
        tester.displaySequenceValues();

    }

    private void createAndModifySequence()
    {
        Node firstNode;
        firstNode = new Node(1);
        firstNode = new Node(3, firstNode);
        firstNode = new Node(5, firstNode);
        firstNode.next = firstNode.next.next;
        head = new Node(7, firstNode);
        head.next.data = 9;
    }

    private void displaySequenceValues()
    {
        Node currentNode = head;
        while (currentNode != null)
        {
            System.out.println(currentNode.data);
            currentNode = currentNode.next;
        }
    }

    //This Node class is now a "nested" class.
    private class Node
    {
        private int data; //Data in the node
        private Node next; //Link to next node

        //Two constructors
        public Node(int data)
        {
            this(data, null);
        }

        public Node
        (
            int data,
            Node next
        )
        {
            this.data = data;
            this.next = next;
        }
    }
}
/*  Note:
    Two class files are produced when you compile this program.
    Exercise: Make the constructors private and re-compile, noting
    that in this case three class files are produced.
*/
/*  Output:
    7
    9
    1
*/

