Last active
August 29, 2015 14:02
-
-
Save SiarheiFedartsou/563c303c8fa698f123ab to your computer and use it in GitHub Desktop.
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
struct Stack<T> : Sequence { | |
struct StackNode | |
{ | |
var value: T | |
var next: Any? = nil | |
} | |
struct StackGenerator : Generator { | |
var top : StackNode? = nil | |
init(top: StackNode?) { | |
self.top = top; | |
} | |
mutating func next() -> T? { | |
var value = self.top?.value | |
self.top = self.top?.next as? StackNode | |
return value ? value! : nil | |
} | |
} | |
var top : StackNode? = nil | |
mutating func push(value: T) { | |
self.top = StackNode(value: value, next: self.top as Any?) | |
} | |
mutating func pop() -> T? { | |
var value = self.top?.value | |
self.top = self.top?.next as? StackNode | |
return value ? value! : nil | |
} | |
func generate() -> StackGenerator | |
{ | |
return StackGenerator(top: self.top) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will it work?