Created
June 15, 2020 14:39
-
-
Save bugrym/1bf5540047242d16420f31cb9ea94abc to your computer and use it in GitHub Desktop.
Generic stack data structure
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
public struct Stack<Element> { | |
private var elements:[Element] = [] | |
public var isEmpty:Bool { | |
peek() == nil | |
} | |
public init() { } | |
public mutating func push(_ element:Element) { | |
return self.elements.append(element) | |
} | |
@discardableResult public mutating func pop() -> Element? { | |
return self.elements.popLast() | |
} | |
public func peek() -> Element? { | |
return self.elements.last | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment