0% found this document useful (0 votes)
11 views

Dequeue Operation

Dequeue Operation

Uploaded by

Azim Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Dequeue Operation

Dequeue Operation

Uploaded by

Azim Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Let's go through the dequeue() function in a simple way and look at

how it works with different iterations (calls to the function) until the
function completes.

Understanding the Setup

Before we start, let's assume we have a queue (a line of elements) that


has the following values:

queue = [10, 20, 30, 40, 50]

front = 0

rear = 4

front = 0: Points to the first element in the queue (10).


rear = 4: Points to the last element in the queue (50).

First Call to dequeue()

1. Checking if the Queue is Empty:

if (front == -1 || front > rear) {

printf("\nQueue Underflow\n");

return;
}

 The function first checks if the queue is empty.

 front == -1: This checks if the queue has never had any elements.
In our case, front = 0, so the queue is not empty.
 front > rear: This checks if all elements have been removed,
leaving the queue empty. In our case, front = 0 and rear = 4, so
the queue is still not empty.

Removing the Element:

item = queue[front];

 The function removes the element at the front of the queue.


 item = queue[front] = queue[0] = 10: The first element 10 is
stored in the variable item.

 Updating the Front and Rear Pointers:

if (front == rear) {

front = -1;

rear = -1;

} else {
front = front + 1;

if (front == rear): This checks if the queue will be empty after this
operation. If front and rear are the same, it means there was only one
element left.

In this case, front = 0 and rear = 4, so they are not equal. Instead, the
function increments front:

front = front + 1; // front becomes 1

The queue now looks like this:

queue = [10, 20, 30, 40, 50]

front = 1

rear = 4

Printing the Removed Element:

printf("\nValue deleted: %d\n", item);


1.
o The function prints the value that was removed: "Value
deleted: 10".

Second Call to dequeue()

1. Checking if the Queue is Empty:


o The queue is still not empty (front = 1 and rear = 4).

2. Removing the Element:


o item = queue[front] = queue[1] = 20: The second element 20
is removed.

3. Updating the Front and Rear Pointers:


o front = front + 1; // front becomes 2
o The queue now looks like this:

queue = [10, 20, 30, 40, 50]

front = 2

rear = 4

1. Printing the Removed Element:


o The function prints "Value deleted: 20".
Third Call to dequeue()

1. Checking if the Queue is Empty:


o The queue is still not empty (front = 2 and rear = 4).

2. Removing the Element:


o item = queue[front] = queue[2] = 30: The third element 30 is
removed.

3. Updating the Front and Rear Pointers:


o front = front + 1; // front becomes 3
o The queue now looks like this:

queue = [10, 20, 30, 40, 50]

front = 3

rear = 4

1. Printing the Removed Element:


o The function prints "Value deleted: 30".

Fourth Call to dequeue()

1. Checking if the Queue is Empty:


o The queue is still not empty (front = 3 and rear = 4).
2. Removing the Element:
o item = queue[front] = queue[3] = 40: The fourth element 40
is removed.

3. Updating the Front and Rear Pointers:


o front = front + 1; // front becomes 4
o The queue now looks like this

queue = [10, 20, 30, 40, 50]

front = 4

rear = 4

1. Printing the Removed Element:


o The function prints "Value deleted: 40".

Fifth Call to dequeue()

1. Checking if the Queue is Empty:


o The queue is still not empty (front = 4 and rear = 4).

2. Removing the Element:


o item = queue[front] = queue[4] = 50: The fifth and last
element 50 is removed.
3. Updating the Front and Rear Pointers:
o Now, front is equal to rear, so both front and rear are set to
-1

front = -1;

rear = -1;

The queue is now empty:

queue = [10, 20, 30, 40, 50]

front = -1

rear = -1

queue = [10, 20, 30, 40, 50]

front = -1

rear = -1

1. Printing the Removed Element:


o The function prints "Value deleted: 50".

Sixth Call to dequeue() (Queue is Empty)

1. Checking if the Queue is Empty:


o Now front = -1 and rear = -1, so the queue is empty.

2. Handling the Empty Queue:


o The function prints "Queue Underflow" because there are
no elements left to remove.
o The function then exits without doing anything else.

You might also like