Iterate through a Linked List in Swift

There’s an IteratorProtocol?

Photo by Emma Matthews Digital Content Production on Unsplash

Prerequisites:

Terminology

Swift terminology

Linked List Terminology

The Motivation

An Iterator

let numbers: [Int] = [1,2,3]
for num in numbers {
print (num)
}

The Linked List Example

Why a Linked List?

The Basic Linked List

My Basic Implementation

Iterating Through Nodes

for element in list {
print(element.item)
}

Conform to the Sequence Protocol

Conform to the Sequence Protocol AND IteratorProtocol

Conforming to the sequence protocol does not guarantee that iterating over the elements is non-destructive

The Sequence Protocol: Repeated Access

Conclusion:

Further study:

The Twitter contact:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store