Week 5: Stack and Queue in Swift

Dylan Chen
Apr 18 · 4 min read
Photo by Mihály Köles on Unsplash

Hi iOS Developers,

Swift has a lot of built-in data structures that allow the program to store a collection of data, such as Array and Set. These two are great, they are very easy to use and have a lot of useful built-in functions and properties. But they are not safe. The elements in Arrays and Sets are randomly accessible. Anyone can access any element at any time, which is insecure in some situations. Here’s where Stack and Queue come in to play.

Unfortunately, Swift doesn’t provide built-in data structures for Stack and Queue, so we need to do that ourselves.

Stack

A Stack is a collection of data that is only allowed to add or remove elements from the end. Elements in a Stack are not randomly accessible. It goes by what looks like an accounting rule: LIFO (Last in, first out).

Photo by Thomas Charters on Unsplash

Unlike arrays, sets and dictionaries , a Stack has very limited functions and properties:

  1. push(_ element: T): Appends an element to the end of the Stack.
  2. peek: T? : Returns the last element of the Stack.
  3. pop() -> T? : Returns the last element and removes it from the Stack.
  4. isEmpty : Returns a boolean determining if the Stack is empty.
  5. count: Returns the number of elements in the Stack

Let’s take a look at the implementation.

Here’s an example of how to use a Stack .

In this example, we are going to implement an ActionViewModel that allows the program to track the history of actions, and allows the program to undo actions.

The reason why we decided to use a Stack over an Array , is because when performing an undo, we want to make sure that we always undo the last action. We don’t want someone to accidentally undo an action that is in the middle or at the beginning of the actions .

Queue

A Queue is very similar to a Stack , but a Queue has an opposite accounting-looking rule: FIFO (First in, first out). Elements in a Queue are also not randomly accessible. We are only allowed to add elements to the end and remove elements from the front of a Queue.

Photo by Dan Counsell on Unsplash

A Queue has very similar functions and properties as a Stack .

  1. enqueue(_ element: T): Appends an element to the end of the Queue.
  2. peek: T? : Returns the first element of the Queue
  3. dequeue() -> T? : Removes and returns the first element of the Queue
  4. isEmpty : Returns a boolean indicating whether the Queue is empty.
  5. count: Returns the number of elements in the Queue

Let’s take a look at the implementation.

Here’s an example of how to use a Queue .

In this example, we are going to implement a distribution feature for a supermarket. Imagine that the supermarket only has one cashier, and only one guest can be checked out at a time. To be fair for all the guests, the supermarket must follow its own version of the FIFO rule, which in this case is “first in the checkout line, first served” (i.e., the guest who gets in the queue first will be served first.)

The reason why we chose a Queue over an Array is that with a Queue , we can prevent someone from “cutting in line.” We always want to serve first whoever gets into the queue first.

Conclusion

Stack and Queue provide a safer, much more secure way to remove and access the elements, but we must follow their rules. The rule for Stack is LIFO Last in, first out), and the rule for Queue is FIFO (First in, first out).

This all I have for week 5. As usual, I’ll be posting another story about iOS development next week. See you then!

Medium Claps — Made in Flinto by Thuy Gia Nguyen on Dribbble

Please clap if you enjoyed this story. Follow me. I’ll see you in subsequent stories :)

YOU MAY ALSO BE INTERESTED IN:

Geek Culture

Proud to geek out.

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dylan Chen

Written by

Senior iOS Engineer. Nothing is better than solving complex problems with clean, elegant code. Buy me a coffee at: https://www.buymeacoffee.com/dylancfe15

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Dylan Chen

Written by

Senior iOS Engineer. Nothing is better than solving complex problems with clean, elegant code. Buy me a coffee at: https://www.buymeacoffee.com/dylancfe15

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium