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

Exp4 - Circular Queue Using Array

The document contains a C program that implements a circular queue using an array of fixed size. It provides functions for enqueueing, dequeueing, and displaying the elements in the queue, along with a simple user interface for interaction. The program handles cases for full and empty queues appropriately.
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)
2 views3 pages

Exp4 - Circular Queue Using Array

The document contains a C program that implements a circular queue using an array of fixed size. It provides functions for enqueueing, dequeueing, and displaying the elements in the queue, along with a simple user interface for interaction. The program handles cases for full and empty queues appropriately.
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

Program: Array Implementation of Circular Queue

#include <stdio.h>
#define SIZE 3
int cq[SIZE], front = -1, rear = -1;

void enqueue() {
int x;
printf("Enter value:");
scanf(" %d", &x);

if ((rear + 1) % SIZE == front)


printf("Queue Full\n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
cq[rear] = x;
}
}

void dequeue() {
if (front == -1)
printf("Queue Empty\n");
else {
printf("Deleted: %d\n", cq[front]);
if (front == rear)
front = rear = -1;
else
front = (front + 1) % SIZE;
}
}

void display() {
int i;
if(front<rear)
{
for (i=front; i<=rear; i++)
printf("%d ", cq[i]);
}
if(front>rear)
{
for (i=front; i<=SIZE-1; i++)
printf("%d ", cq[i]);
for (i=0; i<=rear; i++)
printf("%d ", cq[i]);
}
printf(“\n”);
}

int main() {
int choice;
do
{
printf("Enter your Choice: 1: enqueue \t 2: dequeue \t 3: display \t 4: exit \n");
scanf(" %d", &choice);
if (choice==1) enqueue();
else if (choice==2) dequeue();
else if (choice==3) display();
else if (choice!=4) printf("Enter valid Choice");
}while(choice!=4);
return 0;
}

Output:

Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit


1
Enter value:10
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
1
Enter value:20
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
1
Enter value:30
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
3
10 20 30
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
2
Deleted: 10
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
1
Enter value:40
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
1
Enter value:50
Queue Full
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
3
20 30 40
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
2
Deleted: 20
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit

2
Deleted: 30
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
2
Deleted: 40
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit
2
Queue Empty
Enter your Choice: 1: enqueue 2: dequeue 3: display 4: exit

You might also like