Implement Generic Stack Swift5 - COFPROG

Implement Generic Stack Swift5

How to implement Generic stack in iOS/Swift



//----------------------Generic Stack----------------------//


struct Stack<T> {


    private var stackArr: [T]


    init() { stackArr = [] }


    var isEmpty : Bool { return stackArr.count == 0 }


    var tos : T? { return isEmpty ? nil : stackArr.last }


    mutating func push(_ data: T){ stackArr.append(data) }


    mutating func pop() { if isEmpty { return; }; stackArr.removeLast(); }


}


//----------------------Generic Stack----------------------//





Previous
Next Post »

BOOK OF THE DAY