Created
April 18, 2021 12:18
-
-
Save 9bany/a91ad73515a3e9b7b881a32698e0f048 to your computer and use it in GitHub Desktop.
Stack structure in Swift
This file contains 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>: CustomStringConvertible { | |
var description: String { | |
let title = "==== Stack ==== \n" | |
let body = self.array.map{ "\($0)" }.reversed().joined(separator: "\n") | |
let end = "\n===============" | |
return title + body + end | |
} | |
private var array: [T] = [] | |
mutating func push(_ item: T) { | |
self.array.append(item) | |
} | |
mutating func pop() -> T? { | |
return self.array.popLast() | |
} | |
func peek() -> T? { | |
return self.array.last | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example