I’m just going to do the stupid thing so, Hello World? I want to start blogging, my main goal is going to be once a month until I find a good pace and structure to do it bi-weekly and then weekly.

So for starters I’m just going to share a Linked List, a reason why and a hug in Kirby mode.


Hug in Kirby mode:

<(“)> (>”<)


Why?

I really think that me writing (English not being my main language) and talking about code (I’m still learning) can help me in these two fronts. Also I wanted to try Jekyll, practice more markdown and just drop some ideas from my mind in some place. I will try to not publish without mistakes but I will need time so I’m sorry in advance. In the future I will add some comment section area and other features. Thanks for reading if you are here, really, gracias.


Linked List

No particular reason for this, I just wanted to share some code from the get-go.

import Foundation
/// Let's start with the node, this will be the "box" that encapsulates our value and connects to another node (if any)
class LinkedListNode<T> {
    var value: T
    var nextNode: LinkedListNode?
    init(value: T) {
        self.value = value
    }
}
/// We also need a list that will work as the structure (data structure, duh!, :) ) of the nodes and bootstraps with our first node
class LinkedList<T> {
    var rootNode: LinkedListNode<T>?
}
/// We create the list, state the nodes, and link them all together.
let list = LinkedList<Int>()
let firstNode = LinkedListNode(value: 1)
let secondNode = LinkedListNode(value: 1)
let thirdNode = LinkedListNode(value: 3)
let fourthNode = LinkedListNode(value: 4)
let fifthNode = LinkedListNode(value: 5)
let sixthNode = LinkedListNode(value: 6)
firstNode.nextNode = secondNode
secondNode.nextNode = thirdNode
thirdNode.nextNode = fourthNode
fourthNode.nextNode = fifthNode
fifthNode.nextNode = sixthNode
list.rootNode = firstNode
// We need a class that will be the parent of linked list that has the iterator protocol to check the linkedlist as if it were an array/sequence
class LinkedListIterator<T>: IteratorProtocol {
    private var currentNode: LinkedListNode<T>?
    init(start: LinkedListNode<T>?) {
        currentNode = start
    }
    // We need next to adopt iterator protocol
    // So basically move current one place, meaning, current is the next node, when we reach next again
    // we move current again to the next, and so far until nextNode is nil we are done.
    func next() -> LinkedListNode<T>? {
        print("traversing, calling next")
        currentNode = currentNode?.nextNode
        return currentNode
    }
    /// To avoid setting the firstNode and secondNode with the same value you can use defer
    /// to state first the code before it prints and then we set the next node.
    // func next() -> LinkedListNode<T>? {
    // defer { currentNode = currentNode?.nextNode }
    // return currentNode
    // }
}
// Once the LinkedListIterator is defined we can use it to link it to our LinkedList
extension LinkedList: Sequence {
    func makeIterator() -> LinkedListIterator<T> {
        LinkedListIterator(start: rootNode)
    }
}
// And with that, LinkedList has adopted Sequence with our LinkedListIterator<T>
// Our LinkedListIterator<T> has adopted IteratorProtocol which runs next
// With that we are basically saying, "Traverse this list and give me the value of each node"
// We start with the root node (LinkedListIterator(start: rootNode)), prints the first value
// Calls next on LinkedListIterator, sets the new node and prints value and repeats
// until there is no next and returns nil and the traverse finishes
for node in list {
    print(node.value)
}