Node 0: 223, Node 1: 493 Node 0: 223, Node 1: 493
Several Nodes
can be linked together,
one after another.
Such a structure is called a linked list.
Here is a program that does that:
public class ChainMaker { public static void main ( String[] args ) { // Construct four nodes Node node0 = new Node( 223 ); Node node1 = new Node( 493 ); Node node2 = new Node( -47 ); Node node3 = new Node( 33 ); // Link the nodes into a chain node0.setNext( node1 ); node1.setNext( node2 ); node2.setNext( node3 ); node3.setNext( null ); // not needed. Here as a reminder. // Point p at the first node Node p = node0; System.out.println("Node 0: " + p ); // point p at the second node _______________________ // fill in the blank System.out.println("Node 1: " + p ); } }
Here is the picture:
At the start, the variable p
points at
node0
.
The first println
prints out:
Node 0: 223
Now change p
so that it points to the second Node
(as shown by the dotted line.)
To do this, use the getNext()
method with node0
.
Which of the following statements should fill the blank?
p = p.getNext(); p.getNext();