Created
October 19, 2014 18:48
-
-
Save pragmaticlogic/774c802418edbaed76db to your computer and use it in GitHub Desktop.
Swift simple generic stack
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
| 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