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

6_CircularQueue

Circular queue vtu 3rd sem

Uploaded by

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

6_CircularQueue

Circular queue vtu 3rd sem

Uploaded by

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

/* Lab Program 6

6. Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

*/

#include<stdio.h>
#include<stdlib.h>

#define MAX 5

int front = -1, rear = -1; // global variable


int CQueue[MAX];

void Enqueue();
void Dequeue();
void Display();

int main()
{
int choice;
while(1)
{
printf("\nCIRCULAR QUEUE OPERATIONS USING ARRAY\n\n");
printf("1.INSERT\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("4.EXIT \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:Enqueue();
break;
case 2:Dequeue();
break;
case 3:Display();
break;
case 4:exit(1);
default:printf("\nInvalid Choice\n");
}
}
}

void Enqueue()
{
int item;
if ((front == 0 && rear == MAX - 1) || front == rear + 1)
{ // front==rear + 1 occurs when an element is inserted at
// 0th position due to circular increment and rear value is just
// one less than front i.te front = 1 and rear =0
printf("\nCircular Queue is Full \n");
}

else
{
printf("Enter the element to insert into the Circular Queue :\n ");
scanf("%d", &item);

if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
CQueue[rear] = item;
}
}

void Dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nCircular Queue is Underflow\n");
}
else if(front==rear) //Q has only one element, so we reset the
// queue after dequeue operation
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=(front+1)%MAX;
}
}

void Display()
{
int i;
if (front == -1 && rear == -1)
{
printf("\nThe Circular Queue is Empty \n");

}
else
{
printf("\nElements in a Circular Queue are :");
if (front > rear)
{
for (i = front; i < MAX; i++)
{
printf("%d ", CQueue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", CQueue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", CQueue[i]);
}
}
}

/* OUTPUT

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

Circular Queue is Underflow

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

The Circular Queue is Empty

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
10

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
20

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
30

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :10 20 30


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
40

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
50

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1

Circular Queue is Full

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :10 20 30 40 50


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

The Dequeued Element is 10:


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

The Dequeued Element is 20:


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
90

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
100

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90 100


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1

Circular Queue is Full

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90 100


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 4
*/

/*

// Circular Queue Implementation

#include<stdio.h>
#include<stdlib.h>

#define MAX 5

int front = -1, rear = -1; // global variable


int CQueue[MAX];

void Enqueue();
void Dequeue();
void Display();

int main()
{
int choice;
while(1)
{
printf("\nCIRCULAR QUEUE OPERATIONS USING ARRAY\n\n");
printf("1.INSERT\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("4.EXIT \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:Enqueue();
break;
case 2:Dequeue();
break;
case 3:Display();
break;
case 4:exit(1);
default:printf("\nInvalid Choice\n");
}
}
}

void Enqueue()
{
int item;
if ((front == 0 && rear == MAX - 1) || front == rear + 1)
{ // front==rear + 1 occurs when an element is inserted at
// 0th position due to circular increment and rear value is just
// one less than front i.te front = 1 and rear =0
printf("\nCircular Queue is Full \n");
}

else
{
printf("Enter the element to insert into the Circular Queue :\n ");
scanf("%d", &item);

if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
CQueue[rear] = item;
}
}

void Dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nCircular Queue is Underflow\n");
}
else if(front==rear) //Q has only one element, so we reset the
// queue after dequeue operation
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe Dequeued Element is %d:", CQueue[front]);
front=(front+1)%MAX;
}
}

void Display()
{
int i;
if (front == -1 && rear == -1)
{
printf("\nThe Circular Queue is Empty \n");

}
else
{
printf("\nElements in a Circular Queue are :");
if (front > rear)
{
for (i = front; i < MAX; i++)
{
printf("%d ", CQueue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d ", CQueue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ", CQueue[i]);
}
}
}

/* OUTPUT

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

Circular Queue is Underflow

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

The Circular Queue is Empty

CIRCULAR QUEUE OPERATIONS USING ARRAY


1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
10

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
20

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
30

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :10 20 30


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
40

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
50
CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1

Circular Queue is Full

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :10 20 30 40 50


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

The Dequeued Element is 10:


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 2

The Dequeued Element is 20:


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
90
CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1
Enter the element to insert into the Circular Queue :
100

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90 100


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 1

Circular Queue is Full

CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 3

Elements in a Circular Queue are :30 40 50 90 100


CIRCULAR QUEUE OPERATIONS USING ARRAY

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice : 4
*/

You might also like