Now that you have an understanding of what nodes are, let’s see a Swift implementation!
We will create a basic Node
class that contains data and a link to the next Node
. Our Node
‘s data will be specified as a String
but the same class could easily be changed to hold any other data type. Remember that a Node
‘s link to the next Node
is nil
when there are no more nodes to traverse.
Take a look at the starter code in the editor. You will find the Node
class defined for you.
Instructions
Let’s begin by defining properties for our Node
class.
Since a node contains data, define a variable stored property named data
, of type String
. Then create an initializer with a parameter data
of String
type that sets the class’s data
property to the passed-in data
argument.
Let’s check what we’ve done so far! Below the Node
class, create a new Node
instance named firstNode
initialized with the argument, "I am a Node!"
.
Print out firstNode
‘s data to check if it was set correctly.