Queue

public struct Queue<Element>

First-in-first-out linear data structure.

Remark

Consider using two linked lists instead of an Array as the internal storage. Array gives O(*n*) performance for remove(at:), which is used by dequeue.

Initializers

  • Creates an empty Queue.

    Declaration

    Swift

    public init()

Computed Properties

  • Declaration

    Swift

    @inlinable
    public var isEmpty: Bool { get }

    Return Value

    true if there are no values contained herein. Otherwise, false.

  • Declaration

    Swift

    @inlinable
    public var peek: Element? { get }

    Return Value

    The next value to be dequeued, if it exists. Otherwise, nil.

Instance Methods

  • Adds the value to the Queue.

    Complexity

    O(1) amortized

    Declaration

    Swift

    @inlinable
    public mutating func enqueue(_ value: Element)
  • Removes the next value in the Queue and returns it.

    Warning

    If .isEmpty, dequeue() will crash. If necessary, check that peek returns a non-nil value before dequeueing.

    Complexity

    O(n)

    Declaration

    Swift

    @inlinable
    public mutating func dequeue() -> Element

    Return Value

    The next value in the Queue.