Difference between revisions of "AnotherNodeDemo.java"
From WLCS
(Created page with "<syntaxhighlight lang="Java"> public class AnotherNodeDemo { public static void main(String [] args) { Node n1, n2, n3, n4; //Draw the memory diagram at this point...") |
(No difference)
|
Latest revision as of 11:09, 14 November 2017
public class AnotherNodeDemo
{
public static void main(String [] args)
{
Node n1, n2, n3, n4;
//Draw the memory diagram at this point
n1 = new Node(7);
n1.next = null;
n2 = new Node(86);
n2.next = n1;
//Draw the memory diagram at this point
n4 = new Node();
n3 = new Node();
n4.next = n3;
n3.data = 9;
n4.data = 530;
n1.next = n4;
//Draw the memory diagram at this point
print(n2);
//What is printed?
}
//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 + " ");
}
}
}