Skip to content

Instantly share code, notes, and snippets.

@pragmaticlogic
Created October 19, 2014 18:48
Show Gist options
  • Select an option

  • Save pragmaticlogic/774c802418edbaed76db to your computer and use it in GitHub Desktop.

Select an option

Save pragmaticlogic/774c802418edbaed76db to your computer and use it in GitHub Desktop.
Swift simple generic stack
class Node<T> {
var key: T? = nil
var next: Node? = nil
}
public class Stack<T> {
private var N:Int = 0
private var top: Node<T>! = nil
public func size() -> Int {
return N
}
public func isEmty() -> Bool {
return top === nil
}
public func peek() -> T? {
return top.key
}
private func push(key:T) {
let current:Node<T>! = top
top = Node<T>()
top.key = key
top.next = current
N++
}
public func pop() -> T? {
let item:T? = top.key
top = top.next
N--
return item
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment