Stack

public struct Stack<T>
extension Stack: CustomStringConvertible where T: CustomStringConvertible
extension Stack: CustomDebugStringConvertible where T: CustomDebugStringConvertible

Data Structure used to manage a collection of elements after the LIFO (last in, first out) principle.

  • Creates a new stack with the given elements.

    Declaration

    Swift

    public init(values: T...)
  • Declaration

    Swift

    public var isEmpty: Bool { get }

    Return Value

    true if element count is zero, false otherwise

  • Declaration

    Swift

    public var count: Int { get }

    Return Value

    Number of elements in stack

  • Adds an element on top of the stack

    Declaration

    Swift

    @discardableResult
    public mutating func push(_ element: T) -> Stack<T>

    Parameters

    element

    Element to be pushed on top of stack

    Return Value

    Instance of stack for chaining

  • Removes the last element or if a count is given as many until the stack is empty. Afterwards it returns the last removed element.

    Declaration

    Swift

    @discardableResult
    public mutating func pop(to count: Int? = nil) -> T?

    Parameters

    count

    Amount of elements to remove, can be nil

    Return Value

    Last element which was removed, or nil if stack is empty

  • Returns the element at the given index without changing the stack

    Declaration

    Swift

    public func peek(at index: Int) -> T?

    Parameters

    index

    from bottom up

    Return Value

    element at index or nil if out of bounds

  • top

    Returns the last inserted element

    Declaration

    Swift

    public var top: T? { get }

    Return Value

    Element` or nil if empty

  • Returns the element from the reverse order

    Declaration

    Swift

    public func fromTop(index: Int) -> T?

    Parameters

    index

    distance to most top element

    Return Value

    element at index or nil if out of bounds

Available where T: CustomStringConvertible

  • Declaration

    Swift

    public var description: String { get }

Available where T: CustomDebugStringConvertible

  • Declaration

    Swift

    public var debugDescription: String { get }