Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fifth Edition · iOS 18 · Swift 6.0 · Xcode 16.2

9. Queue Challenges
Written by Vincent Ngo

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Think you have a handle on queues? In this chapter, you will explore five different problems related to queues. This serves to solidify your fundamental knowledge of data structures in general.

Challenge 1: Stack vs. Queue

Explain the difference between a stack and a queue. Provide two real-life examples for each data structure.

Challenge 2: Step-by-step Diagrams

Given the following queue:

J F O Z X

Challenge 3: Whose turn is it?

Open the starter project, and navigate to Challenge 3’s playground page to begin.

Challenge 4: Reverse Queue

Navigate to Challenge 4’s playground page to begin.

Challenge 5: Double-ended Queue

A double-ended queue — a.k.a. a deque — is, as its name suggests, a queue where elements can be added or removed from the front or back.

Note:

In DoubleLinkedList.swift one additional property and function has been added:

Solutions

Solution to Challenge 1

Queues have a behavior of first-in-first-out. What comes in first must come out first. Items in the queue are inserted from the rear and removed from the front.

Solution to Challenge 2

Array

Keep in mind whenever the array is full, and you try to add a new element, a new array will be created with twice the capacity with existing elements being copied over.

T Y A R Z C Q O L Y S alnaioe (“J”) Q G U H M T O uqkaeoi (“E”) X U R H H O xevoaii () H I Q J S A Z oknuoii (“Z”) O B T G U G duvaeuo () F Z H E H juyeaii () D V C U R G omsoeio (“V”)

Linked list

S W I F T S W I F T R enqueue (“R”) W I F T O R C enqueue (“C”) I F T O R C dequeue () F T O R C dequeue () enqueue (K) F T O R C K S W I F T R O enqueue (“O”) dequeue () W I F T R O

Ring buffer

S W I F T r w S W I F T r w S W I F T r w S W I F T r w r w C W I F T r w C W I F T r w C W I F T r w C K I F T enqueue (“R”) return false enqueue (“O”) return false enqueue (“C”) return true enqueue (“K”) dequeue () dequeue () dequeue ()

Double stack

Left Stack S W I F T Right Stack R Left Stack S W I F T Right Stack enqueue (“R”)

Nekj Thays V C E T K B O Vidkz Fqirj X U Z D C T E Mupq Vvogz Qoxwj Zvoxx nexoeei () avxuoei (“I”)

Sugq Dresw A S R K E Z С С Pudmb Gxegb L E L V L U Cilv Zragv Gixzs Xtoyh mesiouo () itlauei (“B”)

Tagb Yjukw O Z V H С С Pilpl Sxunp P E M B I L Jukv Bluvb Kompl Wsujt ifniaoa (“Y”) niviuoa ()

Solution to Challenge 3

Creating a board game manager is straightforward. All you care about is whose turn it is. A queue data structure is the perfect choice to adopt the BoardGameManager protocol!

Solution to Challenge 4

A queue uses first-in-first-out, whereas a stack uses last-in-first-out. You can use a stack to help reverse the contents of a queue. By inserting all the contents of the queue into a stack, you reverse the order once you pop every single element off the stack!

Solution to Challenge 5

Deque is made up of common operations from the Queue and Stack data structures. There are many ways to implement a Deque. You could build one using a circular buffer, two stacks, an array, or a doubly linked list. The solution below makes use of a doubly linked list to construct a Deque.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now