Skip to content

Instantly share code, notes, and snippets.

@bugrym
Created June 15, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugrym/1bf5540047242d16420f31cb9ea94abc to your computer and use it in GitHub Desktop.
Save bugrym/1bf5540047242d16420f31cb9ea94abc to your computer and use it in GitHub Desktop.
Generic stack data structure
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