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

Circular Queue code

The document contains a C program that implements a circular queue using arrays. It includes functions for enqueueing, dequeueing, and displaying the queue, along with a menu-driven interface for user interaction. The program handles cases for an empty queue and a full queue appropriately.

Uploaded by

medidhag54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Circular Queue code

The document contains a C program that implements a circular queue using arrays. It includes functions for enqueueing, dequeueing, and displaying the queue, along with a menu-driven interface for user interaction. The program handles cases for an empty queue and a full queue appropriately.

Uploaded by

medidhag54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

//Circular Queue Implementation using arrays

#include<stdio.h>
#define N 10
int front = -1, rear = -1;
int queue[N];

void enqueue(int x)
{
if(front == -1 && rear == -1)
{
front = rear = 0;
queue[rear] = x;
}
else if((rear+1)%N==front)
{
printf("\nQueue is full");
}
else
{
rear = (rear+1)%N;
queue[rear] = x;
}
}

void dequeue()
{
if(front == -1 && rear == -1)
{
printf("\nQueue is empty");
}
else if(front == rear)
{
front = rear = -1;
}
else
{
printf("\nDequeued element is %d", queue[front]);
front = (front+1)%N;
}
}

void display()
{
int i;
if(front == -1)
{
printf("\nQueue is empty");
return;
}
i = front;
printf("\nQueue is:");
while(i!=rear)
{
printf(" %d", queue[i]);
i = (i+1)%10;
}
printf(" %d",queue[rear]);
}

int main()
{
int choice,x;
printf("\nCircular Queue Menu:");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nENter an element to be insert:");
scanf("%d",&x);
enqueue(x); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: return 0;
default: printf("\nInvalid choice, try again\n");
}
}
}

You might also like