Last active
January 22, 2020 01:58
-
-
Save spurscho/a9f483d92b36741b716644a82e44fd41 to your computer and use it in GitHub Desktop.
Stack_by_array
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<T> { | |
private var elements = Array<T>() | |
public init() {} | |
public mutating func pop() -> T? { | |
return self.elements.popLast() | |
} | |
public mutating func push(element: T) { | |
self.elements.append(element) | |
} | |
public func peek() -> T? { | |
return self.elements.last | |
} | |
public var isEmpty: Bool { | |
return self.elements.isEmpty | |
} | |
public var count: Int { | |
return self.elements.count | |
} | |
} | |
extension Stack: CustomStringConvertible { | |
public var description: String { | |
return self.elements.description | |
} | |
} | |
var myStack = Stack<Int>() | |
for i in 1...5 { | |
myStack.push(element: i) | |
} | |
print(myStack) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment