0% found this document useful (0 votes)
28 views

Code For Circular Queue

This code defines a circular queue data structure and provides functions to create, enqueue, and dequeue elements from the queue. The queue is implemented as a fixed-size array with front and rear pointers. The Enqueue function adds elements to the rear of the queue and handles overflow. The Dequeue function removes elements from the front of the queue and handles underflow. The main function demonstrates using the queue by adding and removing elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Code For Circular Queue

This code defines a circular queue data structure and provides functions to create, enqueue, and dequeue elements from the queue. The queue is implemented as a fixed-size array with front and rear pointers. The Enqueue function adds elements to the rear of the queue and handles overflow. The Dequeue function removes elements from the front of the queue and handles underflow. The main function demonstrates using the queue by adding and removing elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Code for circular Queue

#include <stdio.h>
struct Queue
{
int data[100];
int f,r,size;
};
void createQ(struct Queue *Q,int limit)
{
Q->size=limit;
Q->f=-1;
Q->r=-1;
}
void Enqueue(struct Queue *Q,int x)
{
if(Q->f==-1 && Q->r==-1) // condition to check queue is empty
{
Q->f=0; Q->r=0;
Q->data[Q->r]=x;
}
else if((Q->r+1)%Q->size==Q->f) // condition to check queue is full
printf("Queue is overflow..");
else
{
Q->r=(Q->r+1)%Q->size; // rear is incremented
Q->data[Q->r]=x; // assigning a value to the queue at the rear position.
}
}
Int Dequeue(struct Queue *Q)
{
Int d;
if((Q->f==-1 && Q->r==-1)) // condition to check queue is empty
printf("\n Queue is underflow..");
else if(Q->f== Q->r)
{
d= Q->data[Q->f];
Q->f=-1;
Q->r=-1;
return d;
}
else
{
d= Q->data[Q->f];
Q->f=(Q->f+1)%Q->size;
return d;
}
}
int main(void)
{
struct Queue *Q;
Q=(struct Queue*)malloc(sizeof(struct Queue *));
createQ(Q,3);
Enqueue(Q,11);
Enqueue(Q,22);
Enqueue(Q,33);
Enqueue(Q,44);
Dequeue(Q);
Enqueue(Q,44);
Dequeue(Q);
Dequeue(Q);
Dequeue(Q);
Dequeue(Q);
return 0;
}

You might also like