Practical No.
Aim: Array Implementation of Circular Queue
Theory:
Basically Circular Queue are similarly Queue but a little different shape
of circle. Main difference between circular Queue and linear Queue is
that linear Queue is a straight pointing array and circulate Queue is
circular means we can reinitialize first index while the last one is filled.
It means that we can filled one index multiple Times.
Operations on Circular Queue%The following are the operations that
can be performed on a circular queue:Front: It is used to get the front
element from the Queue.Rear: It is used to get the rear element from
the Queue.enQueue(value): This function is used to insert the new
value in the Queue. The new element is always inserted from the rear
end.deQueue(): This function deletes an element from the Queue. The
deletion in a Queue always takes place from the front end.
Program:
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1;
int rear = -1;
void push(int value) {
if ((rear + 1) % SIZE == front) {
printf("Queue is full\n");
return;
}
if (front == -1) {
front = 0;
}
rear = (rear + 1) % SIZE;
queue[rear] = value;
}
int pop() {
if (front == -1) {
printf("Queue is empty\n");
return -1;
}
int value = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
return value;
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
return;
}
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) {
break;
}
i = (i + 1) % SIZE;
}
printf("\n");
}
int main() {
push(10);
push(20);
push(30);
display();
printf("Popped: %d\n", pop());
display();
push(40);
push(50);
push(60);
display();
return 0;
}
Output: