Stacks

Sejan Miah
Jul 22, 2017 · 2 min read

After having used the data structures arrays and dictionaries comfortably, I’m exploring unfamiliar ones such as Stacks and Queues. In this post we will be focusing on Stacks. Queue up for my post about Queues.

Stacks — follow the last-in-first-out order (LIFO).

1. PUSH — can only push an element to the top of the stack

2. POP — remove the element from the top of the stack

3. PEEK- get a sneak peek at the top element of the stack

Envision a stack as a stack of books, but instead of books, replace that with your type’s content (instead of heart…ha).

In life you don’t want to encounter anything generic, but while writing the code for a Stack, I’ve implemented using generics to foster further reusability.

Let’s do a general run through of what is actually happening. We have a variable array, which has limited access, solely to the file we are using. In that array whenever we add an element, it will be appended as the last element, if you try to remove elements, the last item appended will also be removed. Finally, you can only see the last element in your stack.

Here’s a more entertaining version of Stacks. I created a struct called Lannister and then proceeded to create three objects all of the same type- Lannister. I then pushed these elements into a Lannister Stack and was able to get a description of everything that I had already pushed.

killingMeSwiftly

My experience learning iOS

)
Sejan Miah

Written by

killingMeSwiftly

My experience learning iOS

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade