Skip to content

Instantly share code, notes, and snippets.

@desmondmc
Created March 21, 2017 13:07
Show Gist options
  • Save desmondmc/01ad1e604c45d5b38cc5503bf81ceffe to your computer and use it in GitHub Desktop.
Save desmondmc/01ad1e604c45d5b38cc5503bf81ceffe to your computer and use it in GitHub Desktop.
Linked List Implementation
indirect enum LinkedListNode<T> {
case value(element: T, next: LinkedListNode<T>)
case end
}
extension LinkedListNode: Sequence {
func makeIterator() -> LinkedListIterator<T> {
return LinkedListIterator(current: self)
}
}
struct LinkedListIterator<T>: IteratorProtocol {
var current: LinkedListNode<T>
mutating func next() -> T? {
switch current {
case let .value(element, next):
current = next
return element
case .end:
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment