Queue and circular queue-Array
Queue and circular queue-Array
ENQUEUE
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
DEQUEUE
CIRCULAR QUEUE
ENQUEUE
#include <stdio.h>
#define MAX_SIZE 5
int a[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int x)
{
if (front == -1 && rear == -1)
{
front = rear = 0;
}
else if ((rear + 1) % MAX_SIZE == front)
{
printf("queue is full\n");
return;
}
else
rear = (rear + 1) % MAX_SIZE;
a[rear] = x;
}
DEQUEUE
void dequeue()
{
if (front == -1 && rear == -1)
{
printf("queue is empty \n");
return;
}
else if (front == rear)
{
printf(“%d”,a[front]);
front = rear = -1;
}
Else
printf(“%d”,a[front]);
front = (front + 1) % MAX_SIZE;
}
DISPLAY
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", a[i]);
i=(i+1)%MAX_SIZE;
}
}