Kodeco Forums

Swift Algorithm Club: Swift Queue Data Structure

Learn how to implement a Swift queue, including enqueue, dequeue, and peek, in this step by step tutorial. Includes a downloadable Swift playground!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/848-swift-algorithm-club-swift-queue-data-structure

As I understand isEmpty returns us list.isEmpty.
Why wouldn’t we just use is isEmpty in the func dequeue() instead of list.isEmpty?
guard !isEmpty,…

Can we implement it with array like we are doing with Stack.

struct Queue {

fileprivate var array : [Element] = []

mutating func enque(_ element : Element) {
    array.append(element)
}

mutating func deque() -> Element? {
    return array.removeFirst()
}

}

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]