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
view raw stack.swift hosted with ❤ by GitHub

+ Recent posts