From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Custom queue implementation

Custom queue implementation - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Custom queue implementation

- Here's a Custom Queue Implementation. If you want to follow along with me, you can find the project in the exercise files folder > chapter 11 > 11_2 > end. Queues support first-in first-out semantics for adding and retrieving elements. New elements get added to the back of the queue and we can remove the item least recently appended. Queues don't provide random access to their elements. The node class represents a queue item. The node class is generic, thus it can store any type. It has a property called key of type "T" optional, allowing colors to store any type or a nil value. The second property is a reference to the nextNode. It's type is optional as newly created nodes don't have a nextNode. The initializer sets the key property. The generic queue class has two methods, enqueue appends a new item to the queue. First, it creates a newNode, then it checks if the head property has been set. If the head is nil, that means that our queue was empty, thus the newNode becomes the head.…

Contents