JavaScript — Stacks and Queues

Dhara Patel
3 min readOct 18, 2019

--

Stacks and Queues

Today we will learn about the basics of what a stacks and queue are. Stacks and Queues are ordered lists of things. It can be anything as strings, integers, numbers, or anythings else you guys can think of it. Stacks and Queues each have unique way of adding and removing elements from their data structure.

There are little different in both of that data structure which in Stack, you push elements when you add them and you pop element when you remove them. The other side in Queue, you enqueue elements when you add and dequeue elements when you remove them.

Stacks

Stack Animation!

A stack is a LIFO(last-in-first-out) data structure. LIFo means that the last value that was added to the stack will be the first value out. If we look at stacks as visual representation, they are pretty easy. You can put stuff on the top of the stack, but only take that stuff out first. There are a couple functions that we use in a stack: push, pop, peek and isEmpty.

In day to day life, using Git commands, git stash and git pop are examples of the stacks.

A Stack of Stones

Rock Balancing

Rock balancing is the art of stacking stones on top of the each other without using anything for balance and without them falling. Someone push stones to the top of the stone stack. If they wanted to remove a stone, they would pop the stone off of the top. The stones are added one on top of each other. This is exactly how stacks works as well. You can use stack when you want to store and access data in LIFO order. The last stone you add is the first stone you have access to if you want to remove one.

Queues

Queue Animation!!

Queues are a FIFO(first-in-first-out) data structure. A line in grocery store is good representation of the queue, with the first customer in line being the first to get served, with next in line being served next. Just like a stack, queues have function that allows us to push, pop, peek and checks if the queue isEmpty. Queues provide as with access tot he beginning and the end of the list, respectively called the head and tail.

The real life example would be printing the papers from the computer. The first document sent to printer is going to be print first and come out first.

A queue for food!!

Restaurant Line

The queue data structure stores data in a FIFO order, or first in first out order. One example of this is a line at restaurant, the first person to stand in line is the first one to get served and go out of line. Everyone else queues up behind them. In data structure, we can say people enqueue themselves onto the line, and when they get food they have been dequeued from the line. The first elements enqueued onto a queue is the first element to be removed when dequeued.

we covered what a Stack and Queue data structures are, their principles and functionalities, as well as some common usage examples. Both are very simple and most powerful tools that are use in solving problems as you may find appropriate.

--

--