Swift Stack

Last In First Out

Sridharan T
The Startup
3 min readJun 10, 2020

--

Stack

Stacks are a data structure that is used to hold the data in a particular order.

Consider an example that, you are supposed to arrange a set of books in order. You keep placing the books one above the other. After arranging the books, you can only view the topmost book. If you need to take the bottom-most book, you will have to remove all the books above.

This provides you the order Last In First Out (LIFO). Stacks follow the same order of implementation.

Stack Operations

Push

When you want to add an element onto the stack, you push onto the stack. You may think of it as adding a book on top of the stack of books.

Pushing an element

Pop

When you want to remove an element in the stack, you pop an element of the stack. You may think of it as removing the top book from the stack of books.

Popping an element

Peek

A stack doesn’t allow you to check it’s contents, with the exception of the top element of the stack. A peek method allows you to check what’s on the top of your stack.

Stack Implementation

Create a Stack

we can create a stack in the following way

Here we have declared structure Stack that would contain arrays of strings. We will ll be interacting with the array to implement the push, pop, and peek methods.

Push

Pushing an object onto the stack is relatively straightforward.

This push operation appends the element to the last of the array, not to the beginning.

Pop

Popping the stack is also straightforward.

  • This pop function removes the LAST element using the popLast( ) function available with Swift Arrays.
  • We’ve used an Optional return type String since it can return nil when the stack becomes empty.

Peek

Peeking into the stack is to check the top element of the stack. This method is simple because swift arrays have the last property that returns the last element of stack without mutating itself.

This is how our final structure looks like

CustomStringConvertible

The above example can only hold Strings. Besides, it doesn’t print the stack contents beautifully.

For that, swift has a built-in protocol called CustomStringConvertible that allows us to define how to represent an object as a string.

Write the following under the stack implementation (not inside)

Remove the test code from earlier and write the following one:

The output will be

---Stack---
Networking
Operating System
Object Oriented Programming
Computer Fundamentals
-----------

Generics

Currently, our stack can only store strings. If we want to create a stack to store integers, we will have to implement a whole new stack structure towards integers. But instead of it, Swift provides a functionality called generics.

The syntax is

struct Stack<Element> {
// ...
}

Here the brackets declare the struct as generic. This allows the stack to handle all the data types. So, replace all the String with Element and the final code looks like

Finally, there’s just one change to be made in the description.

// previous
let stackElements = array.reversed().joined(separator: "\n")

// now
let stackElements = array.map { "\($0)" }.reversed().joined(separator: "\n")

NOTE: In the CustomStringConvertible protocol, we were joining the elements using joined. This won’t work for non-string elements. So we need to first map the element to a string using the map operator and then join.

Finally, find the line where we initialized our stack and specialize the stack to type String as

var bookStack = Stack<String>()

This is how our final structure looks like

--

--