[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Circular

Uploaded by

Ashwin Harikumar
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)
12 views5 pages

Circular

Uploaded by

Ashwin Harikumar
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/ 5

PROGRAM: CIRCULAR QUEUE

#include <stdio.h>
#define MAX 3
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue();
void dequeue();
void display();
int main()
{
int ch = 0;
while (ch != 4)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
}
return 0;
}

void enqueue()
{
int item;
if ((rear + 1) % MAX == front)
{
printf("Overflow\n");
}
else
{
printf("Insert the element in queue: ");
scanf("%d", &item);

if (front == -1)
{
front = rear = 0;
}
else
{
rear = (rear + 1) % MAX;
}
queue[rear] = item;
printf("Inserted %d\n", item);
}
}
void dequeue()
{
if (front == -1)
{
printf("Underflow\n");
}
else
{
int item = queue[front];
printf("Deleted %d\n", item);

if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % MAX;
}
}
}

void display()
{
if (front == -1)
{
printf("Queue is empty\n");
}
else
{
printf("Queue elements are: ");
int i = front;
while (i != rear)
{
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("%d", queue[i]);
printf("\n");
}
}
OUTPUT
ubuntu@ubuntu-H81M-S:~/AshwinHarikumar-10$ ./a.out
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 12
Inserted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 14
Inserted 14
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 15
Inserted 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Deleted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements are: 14 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...

You might also like