A circular queue can be implemented through arrays or linked lists. Program 6 gives the array implementation of any circular queue.
Program 6: Array implementation of any Circular queue
include "stdio.h"
void add(int);
void deleteelement(void);
int max=10; /*the maximum limit for queue has been set*/
static int queue[10];
int front=0, rear=-1; /*queue is initially empty*/
void main()
{
int choice,x;
printf ("Enter # for insertion & $ for removal of front the queue and * for exit");
printf("Enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf ("Enter element for insertion in queue:");
scanf("%d",&x);
add(x);
break;
case 2 :
deleteelement();
break;
}
}
void add(int y)
{
if(rear == max-1)
rear = 0;
else
rear = rear + 1;
if( front == rear && queue[front] != NULL)
printf("Queue Overflow");
else
queue[rear] = y;
}
void deleteelement()
{
int deleted_front = 0;
if (front == NULL)
printf("Error - Queue empty");
else
{
deleted_front = queue[front];
queue[front] = NULL;
if (front == max-1)
front = 0;
else
front = front + 1;
}
}