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

Practical - 5 (B) Ravi

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)
9 views3 pages

Practical - 5 (B) Ravi

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/ 3

Program 5(b)

Program Name – Circular Queue Name -Ravi Bhushan Singh


Domain – Queue Roll no- 2300320130191
Problem statement – Write a program for Circular queue implementation
Code :-

#include<stdio.h>
#define maxsize 20
int cq[maxsize], front=-1, rear=-1, item;
int enqueue(){
printf("Enter the element ");
scanf("%d",&item);
if((rear+1)%maxsize==front)
{
printf("circular queue is full\n");
}
else if(front==-1)
{
front=rear=0;
cq[rear]=item;
}
else
{
rear=(rear+1)%maxsize;
cq[rear]=item;
}
}
int dequeue(){
if(front==-1)
{
printf("circular queue is empty");
}
else if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%maxsize;
}
}
int traverse(){
// for(int i=front;i!=rear;i=(i+1)%maxsize)
// {
// printf("%d ",cq[i]);
// }
Program 5(b)
Program Name – Circular Queue Name -Ravi Bhushan Singh
Domain – Queue Roll no- 2300320130191
Problem statement – Write a program for Circular queue implementation
Code :-

// printf("%d ",cq[i]);
if (front == -1) {
printf("Circular queue is empty\n");
} else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", cq[i]);
if (i == rear) break; // Stop when reaching rear
i = (i + 1) % maxsize; // Circular increment
}
printf("\n");
}

}
int main(){
int ch;
while(1)
{
printf("\n 1 enqueue");
printf("\n 2 dequeue");
printf("\n 3 display");
printf("\n 4 exit");
printf("\n Enter your choice ");
scanf("%d ",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break ;
case 3:
traverse();
break ;
case 4:
return 0;
default :
printf("Invalid choice");
}
}
return 0;
}
Time complexity :- O(1)
Space complexity :- O(n)

Program 5(b)
Program Name – Circular Queue Name -Ravi Bhushan Singh
Domain – Queue Roll no- 2300320130191
Problem statement – Write a program for Circular queue implementation
Output :-

You might also like