Last active
March 3, 2020 17:04
-
-
Save Abhishek9634/21a922737e861e134d66cf68476617c3 to your computer and use it in GitHub Desktop.
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
import Foundation | |
class Stack<T: CustomStringConvertible>: CustomStringConvertible { | |
private var array: [T] = [] | |
func pop() -> T? { | |
let popItem = self.array.popLast() | |
print("POP ITEM : \(popItem?.description ?? "")") | |
return popItem | |
} | |
func peak() -> T? { | |
if self.array.isEmpty { return nil } | |
return self.array[self.array.count - 1] | |
} | |
func push(item: T) { | |
print("PUSH ITEM : \(item.description)") | |
array.append(item) | |
} | |
var isEmpty: Bool { | |
return self.array.isEmpty | |
} | |
var count: Int { | |
return self.array.count | |
} | |
var description: String { | |
let top = "### STACK ###\n" | |
let bottom = "\n#############\n" | |
let elements = self.array.reversed().map { $0.description }.joined(separator: "\n") | |
return top + elements + bottom | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment