Stack

public struct Stack<Element>
extension Stack: Sequence
extension Stack: CustomDebugStringConvertible where Element: CustomDebugStringConvertible

A basic stack type implementing the LIFO principle - only the last inserted element can be accessed and removed.

  • Creates an empty Stack.

    Declaration

    Swift

    public init()
  • The number of elements in this stack.

    Declaration

    Swift

    public var count: Int { get }
  • Pushes (appends) an element onto the stack.

    Declaration

    Swift

    public mutating func push(_ element: Element)

    Parameters

    element

    The element to push onto the stack.

  • Removes and returns the last element of the stack.

    Declaration

    Swift

    @discardableResult
    public mutating func pop() -> Element?

    Return Value

    The last element of the stack if the stack is not empty; otherwise, nil.

  • Returns the last element of the stack if the stack is not empty; otherwise, nil.

    Declaration

    Swift

    public func peek() -> Element?
  • Check whether the sequence contains an element that satisfies the given predicate.

    Declaration

    Swift

    public func contains(where predicate: (Element) -> Bool) -> Bool

    Parameters

    predicate

    A closure that takes an element of the sequence and returns whether it represents a match.

    Return Value

    true if the sequence contains an element that satisfies predicate.

  • Modify the last element.

    Declaration

    Swift

    public mutating func modifyLast(by modifier: (inout Element) -> Void)

    Parameters

    modifier

    A function to be applied to the last element to modify the same in place.

  • Declaration

    Swift

    public func makeIterator() -> [Element].Iterator

Available where Element: CustomDebugStringConvertible