Currently, when a Node
is created, it does not refer to its next Node
. Recall that nodes contain links to other nodes, and when the following node is nil
we’ve likely reached the end of the path. To portray this concept, we’ll need a separate property where the reference to the next node will be stored.
With the next
property, we can traverse a sequence of Node
s by visiting a Node
and checking what the next Node
in the sequence is.
Instructions
Inside the Node
class, create a variable stored property, called next
of type optional Node
.
Setting next
as an optional ensures that each new Node
should have its next
property defaulted to nil
.
Let’s check what we’ve done so far! Outside of the Node
class declaration, create a new Node
stored in a variable named nodeOne
with the argument "Node 1"
.
Let’s check what we’ve done so far! On a new line after nodeOne
, create an if-else
statement.
The if
branch should check that the next node of NodeOne
is not nil
using an if-let
construction. If node
is not nil, the if statement should print "var nodeOne = Node(data: "Node 1")"
.
If nodeOne
‘s next node is nil
, the control should go to an else
statement that prints the string "The next node of nodeOne is nil"
.
Now create an additional Node
, called nodeTwo
with the argument "Node 2"
.
On a new line, set nodeTwo
as the next node for "nodeOne"
.
Unwrap the value of the next node and output its contents.