NodeDemo.java
From WLCS
public class NodeDemo
{
public static void main(String [] args)
{
Node head = null;
Node tail = null;
Node n1 = new Node(); //create an instance of the Node object
n1.data = 2017;
n1.next = null;
Node n2 = new Node(2018); //create an instance of the Node object
n2.next = null;
head = n1;
tail = n1;
//What does the memory diagram for this program look like?
System.out.println("head.data => " + head.data);
System.out.println("tail.data => " + tail.data);
n1.next = n2;
tail = n2;
//What does the memory diagram for this program look like now?
System.out.println("head.data => " + head.data);
System.out.println("tail.data => " + tail.data);
System.out.println();
print(head);
System.out.println();
}
//This print method prints out all the nodes connected to each other
public static void print(Node head)
{
//currentNode is a reference that is used to iterate through the nodes
for(Node currentNode = head; currentNode != null; currentNode = currentNode.next)
{
System.out.print(currentNode.data + " ");
}
}
}