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

Circular Queue

Circular queue program

Uploaded by

manuvs0001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Circular Queue

Circular queue program

Uploaded by

manuvs0001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//Circular queue

//Joshy Bovas S3CSEA 43

#include <stdio.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1, rear = -1;
int isFull()
{
return (rear + 1) % MAX_SIZE == front;
}int isEmpty()
{
return front == -1;
}void enqueue(int data)
{
if (isFull())
{
printf("Queue overflow\n");
return;
}if(front == -1)
{
front = 0;
}rear = (rear + 1) % MAX_SIZE;
queue[rear] = data;
printf("Element %d inserted\n", data);
}

int dequeue()
{
if (isEmpty())
{
printf("Queue underflow\n");
return -1;
}

int data = queue[front];


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

return data;
}

void display()
{
if (isEmpty())
{
printf("Queue is empty\n");
return;
}

printf("Queue elements: ");


int i = front;
while (i != rear)
{
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
}

printf("%d\n", queue[rear]);
}

int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
display();
printf("Dequeued element: %d\n", dequeue());
display();
return 0;
}

OUTPUT
Element 10 inserted
Element 20 inserted
Element 30 inserted
Queue elements: 10 20 30
Dequeued element: 10
Queue elements: 20 30
=== Code Execution Successful ===

You might also like