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
import UIKit | |
import Swift | |
public struct Stack<T>{ | |
fileprivate var array=[T]() | |
public var count:Int{ | |
return array.count | |
} | |
public var isEmpty:Bool{ | |
return array.isEmpty | |
} | |
public mutating func push(_ element: T){ | |
array.append(element) | |
} | |
public mutating func pop() ->T?{ | |
return array.popLast(); | |
} | |
public var top: T?{ | |
return array.last | |
} | |
} | |
var stackOfNames=Stack(array: ["Carl","Lisa","Stephanie","Jeff","Wade"]) | |
stackOfNames.push("push_Mike!") | |
print(stackOfNames.array) //"Carl", "Lisa", "Stephanie", "Jeff", "Wade", "push_Mike!"] | |
stackOfNames.pop() | |
print(stackOfNames.array) //["Carl", "Lisa", "Stephanie", "Jeff", "Wade"] | |
print(stackOfNames.top) //Optional("Wade") | |
print(stackOfNames.isEmpty) //false |