C program of circular queue using linked list

C program of circular queue using linked list

7858
0
SHARE

C program of using linked list : A circular queue is one in which the insertion of new element is done at the very first location of the queue if the last location of the queue is full. Suppose if we have a Queue of n elements then after adding the element at the last index i.e. (n-1)th , as queue is starting with 0 index, the next element will be inserted at the very first location of the queue which was not possible  in the simple . That’s why linear queue leads to wastage of the memory, and this flaw of linear queue is overcome by circular queue.

So, in all we can say that the circular queue is a queue in which first element come right after the last element, that means a circular queue has a starting point but no end point.

While dealing with the circular queue we will use the following assumptions :

1. Front will always be pointing to the first element of the queue.
2. If  Front=Rear, the queue will be empty.
3. Each time a new element is inserted into the queue, the value of rear is incremented by one i.e.
Rear = (Rear + 1);
4. Each time when an existing element is deleted from the queue, the value of rear is incremented by one i.e.
Front = (Front + 1);

Operations that can be performed on circular queue :

Like other data sturctures we can perform below operations on circular .

1. Insertion
2. Deletion
3. Traversing

Below is performing following operations like insertion, deletion and traversing.

C program to insert or delete or traverse in circular queue:

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
typedef enum{true,false} bool;
typedef struct{
int front,rear;
int elements[MAX];
}queue;
bool isfull(queue *);
bool isempty(queue *);
void create(queue *);
void enqueue(queue *,int);
void dequeue(queue *);
void peek(queue *);
void trav(queue *);
void main()
{
int choice,element;
queue *pq, q;
create(&q);
while(1){
printf("\n1-enqueue\n2-dequeue\n3-peek\n4-traverse\n5-exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(isfull(&q)) {
printf("queue full");
getch();
}
else{ printf("enter the value");
scanf("%d",&element);
enqueue(&q,element);
}
break;
case 2:
if(isempty(&q)) {
printf("queue empty\nenter any key to continue");
getch();
}
else {
dequeue(&q);
printf("\npress any key to continue");
getch();
}
break;
case 3:
if(isempty(&q)) {
printf("stack empty\nenter any key to continue");
getch();
}
else {
peek(&q);
printf("\npress any key to continue");
getch();
}
break;
case 4:
trav(&q);
printf("\npress any key to continue");
getch();
break;
case 5:
exit(1);
}
}
getch();
}

void create(queue *pq)
{

pq->front=pq->rear=-1;
}
bool isfull(queue *pq)
{
if((pq->front==0)&&(pq->rear==MAX-1)) {
return 1; }
else {
return 0;}
}

bool isempty(queue *pq)
{
if(pq->front==-1) return 1;
else return 0;
}

void enqueue(queue *pq,int element)
{int i;
if(pq->front==-1) pq->front=pq->rear=0;
else if (pq->rear==MAX-1) pq->rear=0;
else pq->rear++;
pq->elements[pq->rear]=element;

}

void dequeue(queue *pq)
{
int temp;
temp=pq->elements[pq->front];
if(pq->front==pq->rear) pq->front=pq->rear=-1;
else if(pq->front==MAX-1) pq->front=0;
else pq->front++;
printf("deleted val is %d",temp);
}

void peek(queue *pq)
{
printf("front element is %d",pq->elements[pq->front]);
}

void trav(queue *pq)
{
int i;
for(i=pq->front;i<=pq->rear;i++)
printf("%d\t",pq->elements[i]);
}```

I hope you all have understood the code and concept of Circular queue. If you have any issues or doubts ask us in form of comments. Happy Coding!

SHARE
Previous articleC program for insertion and deletion in Linear Queue
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com