Link list representation of a circular queue is more efficient as it employs space more competently, of course with the added cost of storing the pointers. Program 7 gives the linked list representation of any circular queue.
Program: Linked list implementation of any Circular queue
#include "stdio.h"
struct cq
{ int value;
int *next;
};
typedef struct cq *cqptr cqptr p, *front, *rear;
main()
{
int choice,x; /* Initialise the circular queue */
cqptr = front = rear = NULL;
printf ("for insertion enter # and for deletion enter $ from the queue")
printf("Enter your choice")
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf ("Enter element for insertion in linked list :");
scanf("%d",&x);
add(&q,x);
break;
case 2 :
delete();
break;
}
}
//* Insertion of element *//
add(int value)
{
struct cq *new;
new = (struct cq*)malloc(sizeof(queue));
new->value = value new->next = NULL;
if (front == NULL)
{
cqptr = new;
front = rear = queueptr;
}
else
{
rear->next = new;
rear=new;
}
}
/*deletion of element */
delete()
{
int delvalue = 0;
if (front == NULL)
{
printf("Queue get empty");
delvalue = front->value;
if (front->next==NULL)
{
free(front);
queueptr = front = rear = NULL;
}
else
{
front=front->next;
free(queueptr);
queueptr = front;
}
}
}