Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 = 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