Created
May 29, 2015 20:50
-
-
Save joshuatbrown/f69cd2a5a0e7308df5c5 to your computer and use it in GitHub Desktop.
Stack in Swift, implemented with a linked list
This file contains 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
class Element<T> { | |
let value: T | |
var next: Element<T>? = nil | |
init(value: T, next: Element<T>?) | |
{ | |
self.value = value | |
self.next = next | |
} | |
} | |
struct LinkedStack<T> | |
{ | |
private var top: Element<T>? | |
mutating func push(o: T) { | |
let element = Element<T>(value: o, next: top) | |
top = element | |
} | |
mutating func pop() -> T? { | |
let result = top?.value | |
top = top?.next | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment