*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)*

**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.

**Â if(rear==MAX-1)**

**{**

**printf(“Queue is full”);**

**}**