Created
March 21, 2017 13:07
-
-
Save desmondmc/01ad1e604c45d5b38cc5503bf81ceffe to your computer and use it in GitHub Desktop.
Linked List Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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