Queue using array

Queue is a linear data structure in which elements can be inserted only from the rear end of the queue and can be deleted only from the front end of the queue.Queue can be implemented by using an array or a linked list.
This tutorial is about implementing queue using an array. 

In the figure above you can see an array which can store up to 6 elements. To implement queue using an array we have to make this array behave like a queue and we can do this by following a property of a queue and the property of a queue is, elements can only be inserted from the rear and can only be deleted from the front end of the queue.

To implement queue using an array first we need to declare an array.
int queue[6];
or
int queue[MAX]              | Here MAX is the size of the array / queue, so if we declare MAX =  6 then it means the size of the array/queue is 6.

Initially the queue will be empty, there will be no elements in the queue as you can see in fig. 1.
Always remember that when the value of  front = -1  then it means that the queue is empty.

 

When an element is inserted into the queue then first the value of rear is incremented by 1 and then the new element gets inserted into the queue from the rear.
These two statements are used to do this.
rear = rear + 1                      (Increments the value of rear by 1)
queue[rear] = element      
(New element gets inserted into the queue from the rear.
Element is the variable which stores the value to be inserted into the queue) 
Initially the value of front and rear is equal to -1 as you can see in fig. 1.

(see  figure 2)
Insert 5:
First the value of rear is incremented by 1 using this statement:
rear = rear + 1
-1 + 1 = 0
Rear becomes equal to 0 and 5 gets inserted  into the queue from the rear with the help of this statement,  queue[rear] = element
First time when an element is inserted into the queue then the value of front from -1 becomes equal to 0 that’s why the value of front is equal to 0. This only happens during the first insertion.

Insert 10:

First the value of rear is incremented by 1 (rear = rear + 1).
0 + 1 = 1
Rear becomes equal to 1 and 10 gets inserted into the queue from the rear (queue[rear]=element).

Delete:
Elements of the queue are always deleted from the front.
To delete an element from the queue the value of front is incremented by 1 using this statement
front = front + 1
You can see in fig. 2 the value of front is equal to 0.
0 + 1 = 1
After incrementing the value, front becomes equal to 1 and 5 gets deleted from the queue.

Delete:

Again the value of front is decremented by 1.
The value of front is equal to 1 so
1 + 1 = 2.
From becomes equal to 2 and 10 gets deleted from the queue.

 Now there are no elements in the queue, the queue has become empty. The value of front = 2 and the value of rear = 1 which means front = rear + 1.
So if the value of   front = rear + 1  then that means, the queue is empty.

So the queue will be empty in two situations.

Situation 1- when the value of  front = -1 
Situation 2- when the value of   front = rear + 1

So in the program we can check if the queue is empty by using this if statement

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

printf(“Queue is empty”);

}

Insert 15:
Value of rear is incremented by 1.
1 + 1 = 2
Rear becomes equal to 2 and 15 gets inserted into the queue.

Insert 20:
Value of rear is incremented by 1.
2 + 1 =3
Rear becomes equal to 3 and 20 gets inserted into the queue.

Insert 25:
Value of rear is incremented by 1.
3 + 1= 4
Rear becomes equal to 4 and 25 gets inserted into the queue.

 

 (see fig. 3)
Insert 30:
Value of rear is incremented by 1.
4 + 1 = 5
Rear becomes equal to 5 and 30 gets inserted into the queue.
Now the queue is full because the value of Rear is equal to 5. Rear is at the last position of the queue so now we cannot insert any new element into the queue.
So when  rear = MAX – 1  then it means that the queue is full.
MAX-1 means last position of the queue because the value of MAX in this case is 6 and 6-1 is equal to 5.
In the program we can check if the queue is full by using this if statement.
 if(rear==MAX-1)

{
printf(“Queue is full”);
}

Previous Post
C Program to implement stack using array
Next Post
Queue using array program blueprint

Related Posts

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu