Drawback of Queue using Array

Drawback of Queue using Array:

In this article, I will discuss the Drawbacks of Queue using Array. Please read our previous article on how to Implement a Queue using an Array in C Language with Examples. In our previous article, we discussed implementing a queue using two pointers (front and rear) in an array. In this article, we will discuss some drawbacks of the two-pointer method.

Drawback of Queue using Array

Let’s start with discussing the drawbacks.

Here, we have a queue of size 7 (array of size 7). It has some elements. The rear is pointing to the 6th index, and the front is pointing to the 2nd index. As the rear is pointing at the 6th index, which is the size of the queue – 1, it means the queue is full. There is no space for inserting more elements. 8 is the first element in the queue, so the front is pointing just before the first element. So, what is the drawback?

Now, we want to insert a new element in the queue. If we try to insert through the enqueue operation, we’ll get a message “queue is full.” So, we cannot insert a new element. But if you see the queue, it has some free spaces (spaces of deleted elements). We’re unable to use these free spaces because the insertion is done from the rear end, and there is no space at the rear end. Spaces are available on the front side, so we cannot utilize these spaces. The first drawback is that we cannot reuse the spaces of deleted elements. Suppose we delete a few more elements.

first drawback is that we cannot reuse the spaces of deleted elements

Now, this queue has only one element, which is 9. Still, many spaces are vacant. If we try to insert more elements, we again get a message that “queue is full” because the rear is equal to size – 1 (7 – 1 = 6). The queue full condition is satisfied. We cannot insert more elements even though a lot of space is free in the queue. The second drawback is that every location can be used only once. We cannot reuse them. If we delete the remaining element,

second drawback is that every location can be used only once

Now, the front and rear are pointing at the same location, which is the condition for an empty queue. So, the queue is empty. Can we insert any element? No, the queue is full because the rear is equal to size – 1. The third drawback is a situation where the queue is empty as well as full.

We discussed three drawbacks of using an array for a queue. Array spaces can be used only once, but we want to reuse them. So, how can we reuse them? The first solution to this problem is resetting the pointers.

Solutions for the Space Problem in an Array:
Resetting Pointers:

In this approach, if the queue becomes empty at any time (at any place), we’ll bring the front and rear pointers to the beginning of the array, or we can say that we initialize the rear and front pointers to -1. In this way, we can reuse the space in an array. Suppose we have three elements in the array.

Solutions for the Space Problem in an Array

Now, we want to delete two elements.

Solutions for the Space Problem in an Array

After deleting two elements, the front is pointing at index 1. Now, let’s delete one more element.

Drawback of Queue using Array

Here, both front and rear are at the same place and pointing at the same index, which means the queue is empty. So, we have to reinitialize them to -1.

Drawback of Queue using Array

This is what we will do in this approach. So whenever the front and rear become equal, which means the queue becomes empty, we initialize them to -1 so that we can reuse the spaces in the array. But this method doesn’t guarantee that these spaces will be reused definitely. If a queue does not become empty at any time, then we cannot reset it, and we cannot insert more elements. So, we need another solution to the space problem. The second method is a Circular Queue, which we will discuss in our next article.

In the next article, I will discuss How to Implement a Circular Queue in C Language with Examples. In this article, I explain the drawbacks of Queue using Array in C Language with Examples. I hope you enjoy this Drawback of Queue using Array article.

Leave a Reply

Your email address will not be published. Required fields are marked *