0% found this document useful (0 votes)
46 views3 pages

Circular Queue

This C program implements a circular queue using an array. It defines functions for enqueue, dequeue, display, isFull, and isEmpty operations on the queue. The main function initializes the queue size, and provides a menu-driven interface for the user to select these queue operations and test the implementation.

Uploaded by

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

Circular Queue

This C program implements a circular queue using an array. It defines functions for enqueue, dequeue, display, isFull, and isEmpty operations on the queue. The main function initializes the queue size, and provides a menu-driven interface for the user to select these queue operations and test the implementation.

Uploaded by

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

// implementation of circular queue using array.

#include <stdio.h>
#include <stdlib.h>
int *queue,n,rear=-1,front =-1;
void enqueue();
void deque();
void display();
void isFull();
void isEmpty();

int main(){
int ch;
printf("Specify the size of queue : ");
scanf("%d",&n);
printf("\n\n\n***Operations on the Cirular Queue***\n\n\n");
queue = (int*) malloc(n*sizeof(int));
do{
printf("\nEnter your choice.\n1.Insert\n2.Delete\n3.Display\n4.IsFull\
n5.IsEmpty\n6.Exit\n::");
scanf("%d",&ch);
switch(ch){
case 1:
enqueue();
break;

case 2:
deque();
break;

case 3:
display();
break;

case 4:
isFull();
break;

case 5:
isEmpty();
break;

case 6:
printf("Exited from program successfully.");
break;

default :
printf("Please make a valid choice.from(1/2/3/4/5/6/7)");
}
}while(ch!=6);
return 0;
}

void enqueue(){
int item;
if ((front == 0 && rear == n - 1) || (front == rear + 1))
{
printf("queue is full");
return;
}
else if (rear == - 1)
{
rear++;
front++;
}
else if (rear == n - 1 && front > 0)
{
rear = 0;
}
else
{
rear++;

}
printf("\nEnter an element : ");
scanf("%d",&item);
queue[rear] = item;
return;
}

void deque(){
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
else
{
printf("\n %d deleted", queue[front]);
front++;
}
return;
}

void display(){
int i;
if(front == - 1){
printf("\n\n***There is nothing in queue to display.***\n\n");
}else{
if (front > rear)
{
for (i = front; i < n; i++)
{
printf("%d ", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
}}
return;
}
void isFull(){
if((front == rear + 1) || (front == 0 && rear == n - 1)){
printf("\nYes, Queue is full.\n");
}else{
printf("\nNo, Queue is not full yet.\n");
}
return;
}

void isEmpty(){
if(front == -1){
printf("Yes, Queue is Empty.\n");
}else{
printf("No, Queue is not Empty.\n");
}
return;
}

You might also like