What is Queue in Data Structure?

Faizah Ahsan
3 min readNov 28, 2020

--

What is it Data Structure?

Data structures are ways of organizing information with optimal ‘runtime complexity’ for adding or removing records. So, in other words, these different data structures that I will show you all have very optimized ways of accessing or editing data.

In a context of an interview, usually a lot of these data structures questions are really themed around runtime complexity. If someone says to you, ‘Hey, write some function that does xyz’. One of the first thing you want to think of is what data structure you can use to solve this problem, that has some optimal runtime complexity of what you are trying to solve.

The second thing you want to understand is that Javascript natively implements several data structures.

In this diagram, the big blue circle is demonstrating what a Javascript array has the capability to do. A javascript array has a tremendous number of different things.

The tiny little white circle of functionality at the bottom is what a queue does. So, what this diagram means is that, say a javascript array does everything that classic queue data structure does.

In a case of a queue, we are going to use a Javascript array to represents queue.

What is a Queue?

A queue can be thought of as a container of sorts, where records or pieces of data enter on one end of the container and then exist on the other. You can really think of the queue as being very much like waiting in line to buy tickets from some ticketing counter.

In this particular example you can think of the line of people as a queue. Because a person will enter into the line from one end and exit from the other. In a queue there is no idea of skipping or cutting in line. So, the order in which you get into the queue also dictates the order in which you come out.

The process of adding a record into a queue is referred to as Enqueuing. Taking something out from the other end is referred as Dequeuing.

In, the diagram below you can see how it works:

You can implement this first in first out method in a queue or array problem by using Shift, unshift, push, pop, splice, slice methods.

Here is a solution of a queue problem:

--

--

Faizah Ahsan
Faizah Ahsan

Written by Faizah Ahsan

Junior Full-Stack Web Developer. Looking for opportunities!

No responses yet