Queue

public struct Queue<T> : Collection, ExpressibleByArrayLiteral

Standard queue (FIFO) - items are added at one end of the queue, and removed from the other. add() and remove() are O(1) (amortized for remove()). Queue conforms to the Collection and ExpressibleByArrayLiteral protocols.

var q = Queue<Int>()
q.isEmpty             // true
q.add(3)
q.add(7)
q.peek()              // 3
q.count               // 2
q[0]                  // 3
q[1]                  // 7
let val = q.remove()  // val = 3
q.peek()              // 7
q.contains(7)         // true
  • Declaration

    Swift

    public typealias ArrayLiteralElement = T
  • Declaration

    Swift

    public var startIndex: Int { get }
  • Declaration

    Swift

    public var endIndex: Int { get }
  • Constructor using array literal.

    Declaration

    Swift

    public init(arrayLiteral elements: T...)
  • Adds (enqueues) an item. Complexity is O(1).

    Declaration

    Swift

    public mutating func add(_ item: T)

    Parameters

    item

    item to be added

  • Removes (dequeues) an item from the opposite end of the queue from which items are added. Complexity is O(1) (amortized).

    Declaration

    Swift

    @discardableResult
    public mutating func remove() -> T?

    Return Value

    optional item (nil if the queue is empty)

  • Return the next item to be removed (dequeued) without actually removing it. Complexity is O(1).

    Declaration

    Swift

    public func peek() -> T?

    Return Value

    optional item (nil if the queue is empty)

  • Declaration

    Swift

    public subscript(pos: Int) -> T { get }
  • Declaration

    Swift

    public func index(after i: Int) -> Int