Data Structures Pt. 2 “Stacks”

Jared Matta
3 min readAug 31, 2020

--

If you are in the process of your job search and job interviews, I am sure you’ve come across the word Data Structures. What are they and how can we use them in our daily programming. I’ve started a blog series on Data Structures, touching on a different type each week. We will be using Javascript as the programming language as we dive into Data Structures. Last time we discussed Queues, this time we will talk about Stacks. One thing I would like to add is while data structures are common in a tech interview, we find ours mostly working with arrays and objects, but they are important for us to understand on an algorithm level. So let’s get started.

What are Data Structures? Data structures are all about runtime complexity, and there are two main things we need to know. Data structures are ways of organizing information with optimal ‘runtime complexity’ for adding and or removing records.

What is a Stack? In general, a stack is very similar to a queue, with one big exception. In a stack, we are still dealing with an ordered list of records, in a container, via the stack. Adding a record to an existing stack is referred to as pushing while removing a record is called popping. The main difference between a stack and a queue is the order in which items are added and removed. If you recall in a queue the order was referred to as FIFO or First in — First out. With a stack the order is known as FILO or First in — Last out.

Writing a stack: We start by creating a class called Stack with a constructor function that allows us to initialize our array from the implementation of an object. Very similar to the queue data structure already.

Methods of a stack: Next we follow up by adding our methods for our stack class, which again are eerily similar to the queue methods. This time we will add the peek method to go the extra mile, although not necessary. Now all of our code will look very similar to the queue methods but as you can see we use pop instead of unshifting in our add method, to declare the First in Last out protocol. Along with the class name, that’s the only difference.

Calling the Stack: Once all of our methods are written out, we can create a new instance of the stack, and call our methods on it. In the console, we can see that we are following the FILO call, and we can also use peek to view or last recorded added in the stack.

Congratulations we have created a successful stack. Easy right, especially if you compare it to the Queue Data Structure, there’s only a difference of the unshift() method to push(). As you can see in both stacks and queues we are working with arrays, without using actual array methods and implementation. These are fairly common and next, we will be moving on to hash tables. I hope you enjoyed it and stay tuned.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jared Matta
Jared Matta

Written by Jared Matta

Flatiron Graduate, Full-Stack developer on his job search. I love working with Javascript and React.

No responses yet

Write a response