Skip to content

Instantly share code, notes, and snippets.

@joshuatbrown
Created May 29, 2015 20:50
Show Gist options
  • Save joshuatbrown/f69cd2a5a0e7308df5c5 to your computer and use it in GitHub Desktop.
Save joshuatbrown/f69cd2a5a0e7308df5c5 to your computer and use it in GitHub Desktop.
Stack in Swift, implemented with a linked list
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