If a Dequeue is implemented via arrays, then this will suffer with the similar problems which a linear queue had suffered. Program 8 gives the array implementation of Dequeue.
Program: Array implementation of a Dequeue
#include "stdio.h"
#define QUEUE_LENGTH 10;
int dq[QUEUE_LENGTH];
int front, rear, choice,x,y;
main()
{
int choice,x;
front = rear = -1; /* initialize the front and rear to null i.e empty queue */
printf ("enter 1 for insertion of any element and 2 for eliminate element from the front of the queue");
printf ("enter 3 to add needed element and 4 for eliminate element from the rear of the queue"); printf("Enter your option");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf ("Enter element to be added :");
scanf("%d",&x);
add_front(x);
break;
case 2:
delete_front();
break;
case 3:
printf ("Enter any element to insertion :");
scanf("%d ",&x);
add_rear(x);
break;
case 4:
delete_rear();
break;
}
}
/**************** Insertion at the front ***************/
add_front(int y)
{
if (front == 0)
{
printf("At the front position element cannot be inserted ");
return;
else
{
front = front - 1;
dq[front] = y;
if (front == -1 ) front = 0;
}
}
/**************** Deletion from the front ***************/
delete_front()
{
if front == -1
printf("Queue empty");
else
return dq[front];
if (front = = rear)
front = rear = -1
else
front = front + 1;
}
/**************** Insertion at the rear ***************/
add_rear(int y)
if (front == QUEUE_LENGTH -1 )
{
printf("At the rear , element cannot be inserted ")
return;
else
{
rear = rear + 1;
dq[rear] = y;
if (rear = = -1 )
rear = 0;
}
}
/**************** Delete at the rear ***************/
delete_rear()
{
if rear == -1
printf("deletion is not attainable at rear position");
else
{
if (front = = rear)
front = rear = -1
else
{
rear = rear - 1;
return dq[rear];
}
}
}