Data Structure: Stack and Queue in Javascript

Osgood Gunawan
3 min readAug 10, 2020

--

I know there are many people already writing about data structures topics out there, but I believe the best and fast way to learn for me is to write an article about it. For that reason, I decided to have a journal of each data structure topic.

Data structures are crucial topics that you often get asked in the technical interview but also help your problem-solving skills in algorithms. There are many ways of solving an algorithm question to get the result. Sometimes you could solve it the hard or easy way, and it all depends on your data structures knowledge. Or sometimes, the solution is not optimized due to not knowing the fundamental concept of data structures. So, let’s start learning the two most common data structures in computer science Stack and Queue.

What is a Stack?

The last element added to the stack will be the first element removed from the stack. Last In First Out (LIFO) method mantra is what stack is known. In real life, examples of the stack could be a pile of plates, books, and papers. To get the middle of a collection of the book, you will need to remove the book on top of it first. It means that the last book that comes in a pile will be the first book to get removed from the collection (the top of the book). Stacks used where:

  • Managing function invocations ,recursion function
  • Undo/Redo, (those “control + z” or “command + z”)
  • Routing route(the history object) in front end framework such as react

Now that we understand the concept let’s implement a stack with an array. There is more that one way of achieving a stack.

Here is my version:

Stack implementation in array

What is a Queue?

A Queue is similar to stack but works the opposite way. First In First Out (FIFO) the same as Last In Last Out (LILO). In terms of real-life situations, think about waiting in a line at the DMV. The first person usually gets in is the first person often to get out. Queues used where:

  • In an online video game where someone will join last, and someone will join first.
  • Uploading resources (download and upload something in your computer depending on the size of the file)
  • Background tasks on your computer
  • Printing/Task processing (a printer can only handle one thing at a time on a time, and whoever hit the print first in the public library that person will get his/her stuff print first)

There are two terms in a queue; an enqueue and dequeue. An enqueue is the insertion, and a dequeue is the deletion of an element. Now we have the bigger picture, let’s dive into the implementation part. Again, there are many ways of implementing queues.

Here is my version:

Queue implementation in array

These are stack and queue implementation in an array. Next journal, I will talk about Linked List, where I will implement a stack and queue in Linked list. Thanks for reading and I hope you can learn something from the article.

--

--

Osgood Gunawan
Osgood Gunawan

Written by Osgood Gunawan

UX designer | Software Engineer | Dancer | ETL Developer | Data Migration. More about me : https://www.osgood1024.com/

No responses yet