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‘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
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.
Let’s begin by defining properties for our
Since a node contains data, define a variable stored property named
data, of type
String. Then create an initializer with a parameter
String type that sets the class’s
data property to the passed-in
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!".
firstNode‘s data to check if it was set correctly.