Data Structure and Algorithms – Queue

Taylor Emma
6 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

Queue Example

A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.

Queue Representation

As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure βˆ’

Queue Example

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

Basic Operations

Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues βˆ’

Β·Β Β Β Β Β Β Β Β enqueue()Β βˆ’ add (store) an item to the queue.

Β·Β Β Β Β Β Β Β Β dequeue()Β βˆ’ remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation efficient. These are βˆ’

Β·Β Β Β Β Β Β Β Β peek()Β βˆ’ Gets the element at the front of the queue without removing it.

Β·Β Β Β Β Β Β Β Β isfull()Β βˆ’ Checks if the queue is full.

Β·Β Β Β Β Β Β Β Β isempty()Β βˆ’ Checks if the queue is empty.

In queue, we always dequeue (or access) data, pointed byΒ frontΒ pointer and while enqueing (or storing) data in the queue we take help ofΒ rearΒ pointer.

Let’s first learn about supportive functions of a queue βˆ’

peek()

This function helps to see the data at theΒ frontΒ of the queue. The algorithm of peek() function is as follows βˆ’

Algorithm

begin procedure peek
Β Β  return queue[front]
end procedure

Implementation of peek() function in C programming language βˆ’

Example

int peek() {
Β Β  return queue[front];
}

isfull()

As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function βˆ’

Algorithm

begin procedure isfull
Β 
Β Β  if rear equals to MAXSIZE
Β Β Β Β Β  return true
Β Β  else
Β Β Β Β Β  return false
Β Β  endif
Β Β  
end procedure

Implementation of isfull() function in C programming language βˆ’

Example

bool isfull() {
Β Β  if(rear == MAXSIZE - 1)
Β Β Β Β Β  return true;
Β Β  else
Β Β Β Β Β  return false;
}

isempty()

Algorithm of isempty() function βˆ’

Algorithm

begin procedure isempty
Β 
Β Β  if front is less than MINΒ  OR front is greater than rear
Β Β Β Β Β  return true
Β Β  else
Β Β Β Β Β  return false
Β Β  endif
Β Β  
end procedure

If the value ofΒ frontΒ is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.

Here’s the C programming code βˆ’

Example

bool isempty() {
Β Β  if(front < 0 || front > rear) 
Β Β Β Β Β Β return true;
Β Β  else
Β Β Β Β Β  return false;
}

Enqueue Operation

Queues maintain two data pointers,Β frontΒ andΒ rear. Therefore, its operations are comparatively difficult to implement than that of stacks.

The following steps should be taken to enqueue (insert) data into a queue βˆ’

Β·Β Β Β Β Β Β Β Β Step 1Β βˆ’ Check if the queue is full.

Β·Β Β Β Β Β Β Β Β Step 2Β βˆ’ If the queue is full, produce overflow error and exit.

Β·Β Β Β Β Β Β Β Β Step 3Β βˆ’ If the queue is not full, incrementΒ rearΒ pointer to point the next empty space.

Β·Β Β Β Β Β Β Β Β Step 4Β βˆ’ Add data element to the queue location, where the rear is pointing.

Β·Β Β Β Β Β Β Β Β Step 5Β βˆ’ return success.

Insert Operation

Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations.

Algorithm for enqueue operation

procedure enqueue(data)Β Β Β Β Β  
Β Β Β 
Β Β Β if queue is full
Β Β Β Β Β  return overflow
Β Β  endif
Β Β  
Β Β Β rear ← rear + 1
Β Β  queue[rear] ← data
Β Β  return true
Β Β  
end procedure

Implementation of enqueue() in C programming language βˆ’

Example

int enqueue(int data)Β Β Β Β Β  
Β Β Β if(isfull())
Β Β Β Β Β  return 0;
Β Β  
Β Β Β rear = rear + 1;
Β Β  queue[rear] = data;
Β Β  
Β Β Β return 1;
end procedure

Dequeue Operation

Accessing data from the queue is a process of two tasks βˆ’ access the data whereΒ frontΒ is pointing and remove the data after access. The following steps are taken to performΒ dequeueΒ operation βˆ’

Β·Β Β Β Β Β Β Β Β Step 1Β βˆ’ Check if the queue is empty.

Β·Β Β Β Β Β Β Β Β Step 2Β βˆ’ If the queue is empty, produce underflow error and exit.

Β·Β Β Β Β Β Β Β Β Step 3Β βˆ’ If the queue is not empty, access the data whereΒ frontΒ is pointing.

Β·Β Β Β Β Β Β Β Β Step 4Β βˆ’ IncrementΒ frontΒ pointer to point to the next available data element.

Β·Β Β Β Β Β Β Β Β Step 5Β βˆ’ Return success.

Remove Operation

Algorithm for dequeue operation

procedure dequeue
Β Β  
Β Β Β if queue is empty
Β Β Β Β Β  return underflow
Β Β  end if
Β 
Β Β  data = queue[front]
Β Β  front ← front + 1
Β Β  return true
Β 
end procedure

Implementation of dequeue() in C programming language βˆ’

Example

int dequeue() {
Β Β  if(isempty())
Β Β Β Β Β  return 0;
Β 
Β Β  int data = queue[front];
Β Β  front = front + 1;
Β 
Β Β  return data;
}
Share This Article
A senior editor for The Mars that left the company to join the team of SenseCentral as a news editor and content creator. An artist by nature who enjoys video games, guitars, action figures, cooking, painting, drawing and good music.
Leave a review