Stacks & Queues

Data Structures can be cool.

Josh A
4 min readJan 31, 2019

Prerequisites🧐

Programming languages have the ability to use really useful array objects that create basic data structures. Two of the most common ones are stacks and queues! This blog will dive into these concepts and relate them to the real world for basic understanding.

Array: an array is a random access data structure that allows each element to be accessed directly and in constant time. How we use books is similar to how arrays work. Each page of the book can be opened independently of the others.

Linked list: and ordered set of data elements, each containing a link to its successor.The elements are not stored in contiguous memory locations.Elements in linked list can only be accessed in a particular order. to learn more visit https://www.geeksforgeeks.org/data-structures/linked-list/.

Now lets start with stacks📚

This stack looks beautiful 😍.

A stack is a container of objects. These objects are inserted(pushed) and removed(popped) according to the principle of Last In First Out or as programmers would say LIFO. Stacks only allow the two operations push which adds an item and pop that removes the item. Stacks only work from the top down. Lets use the gif as a helpful analogy. Lets say your best friend made you pancakes this morning. He/she sets a plate on the table full of pancakes one stacked on top of the other. How would you approach this stack? Well you’d start by taking the first pancake at the top of the stack(pop). You eat a few more pancakes on the stack and theres one more left. You and your friend want seconds but theres not enough food on the plate. He/she adds more pancakes to the stack(push). you wont be able to access the remaining pancake form the pervious stack unless removed all the new pancakes on top of it. Another cool fact about stacks is that they are recursive data structures! Stacks are either empty or it consists of a top and the rest which is a stack.

Stack in action!

Lets use javascript for our stack in action example. First we set up an empty array. Remember stacks can be empty! Next we use .push to add our items to the array. Now, if we want to remove these items we use .pop. .pop will remove the last item added to the stack and so on.

Not so bad right? So what are some coding examples you’d see a stack used? The simplest application of a stack is to reverse a word. You push a given word to stack — letter by letter — and then pop letters from the stack. Another application is an “undo” mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.

Queues!🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️🚶🏾‍♂️

A queue is a container of objects that are inserted and removed according to the First in First out principles. Programmers call this FIFO!Queues are just as relatable to real life as stacks! The best way to explain queues is by example. Lets say you have a line of customers that just ordered their food. Whenever a new person decides they’d also like food they go to the back of the line. As each customer receives their food they get off the line. Queues have two operations, enqueue and dequeue. Enqueue is an operation that inserts an item into the back of the queue and dequeue removes an item from the front of the queue. The difference between stacks and queues are how items are moved. In stacks we remove our most recent item but in queues we remove our oldest item. Real simple you use these methods in real life… EVERYDAY.

Queues in action

Once again for our example we will be using javascript! Once again we start with an empty array. We need to add items to our array so lets use .push. After we added our items lets say we’d like to remove something from the queue. Use the .shift method so we can remove that first element in the array!

Conclusion

Stacks and queues are simple concepts that have a lot of power. If you’d like to check out some visual representations look at https://visualgo.net/en/list?slide=1!

--

--