Site icon Study Algorithms

What is a Queue?

A queue is a linear data structure that stores data (similar to Linked Lists and Stacks). In a queue, the order in which the data arrives is important.

Definition: A queue is an ordered list in which insertions are done at one end (rear) and deletions are done at other end (front). The first element that you insert, is the first one that is removed. This makes it a First In First Out (FIFO) or Last In Last Out (LILO) list.

Basic Operations:

The basic operations on this data structure are:

Fig: Basic operations on a queue

Trying to DeQueue an empty queue is called as underflow and trying to EnQueue an element when it is full, is called as overflow. Generally, we treat them as exceptions. As an example, consider this snapshot:

Fig: Snapshot of a queue data structure

How does it look like?

A line at reservation counter (movies/airports) explains the concept of a queue. When you enter the line you put yourself at the end of the line.

The person who is standing a the FRONT of the line will be first one to buy the ticket. This person then exits from the “FRONT”.

A little later the head/front of the queue will update and the next person in line would become the head. This will keep on happening until we reach the end/REAR of the queue, and everyone has been served. This is an example of a use case where there is need to maintain the order of arrival.

Direct Applications:-

Indirect Applications:-

Video Explanation:

Exit mobile version