One of the exercises in Paul Hudson's book Swift Coding Exercises is "create a linked list of lowercase English alphabet letters, along with a method that traverses all nodes and prints their letters on a single line separated by spaces.”
He further adds
Hint #1: If your first instinct was to create your new data types as a struct, it shows you’re a sound Swift developer. Sadly, I’m afraid that approach won’t work here because structs can’t have stored properties that reference themselves.
However in John Sundell's article Picking the right data structure in Swift he implements it as a value type
Let’s start by declaring a
List
struct, which will keep track of the first and last nodes within our list. We’ll make both of those properties read-only outside of our type, to be able to ensure data consistency:
struct List<Value> {
private(set) var firstNode: Node?
private(set) var lastNode: Node?
}
extension List {
class Node {
var value: Value
fileprivate(set) weak var previous: Node?
fileprivate(set) var next: Node?
init(value: Value) {
self.value = value
}
}
}
Is this a mistake on Sundell's part?