What is Circular Queue?
The queue is considered a circular queue when the positions 0 and MAX-1 are adjacent. Any position before front is also after rear.
There are five operations using count:
- Initialization.
- Addition or insertion.
- Deletion.
- Is_full check.
- Is_empty check.
There are five types of implementation :
- INIT(QUEUE,FRONT,REAR,COUNT)
- INSERT-ITEM(QUEUE, FRONT, REAR, MAX, COUNT, ITEM)
- REMOVE-ITEM(QUEUE, FRONT, REAR, COUNT, ITEM)
- FULL-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,FULL)
- EMPTY-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,EMPTY)
-----------------------------------------------------------------
INIT(QUEUE,FORNT,REAR,COUNT)
This algorithm is used to initialize circular queue.
1. FRONT := 1;
2. REAR := 0;
3. COUNT := 0;
4. Return;
-----------------------------------------------------------------
INSERT-ITEM( QUEUE, FRONT, REAR, MAX, COUNT, ITEM)
This algorithm is used to insert or add item
into circular queue.
1. If ( COUNT = MAX ) then
a. Display “Queue overflow”;
b. Return;
2. Otherwise
a. If ( REAR = MAX ) then
i. REAR := 1;
b. Otherwise
i. REAR := REAR + 1;
c. QUEUE(REAR) := ITEM;
d. COUNT := COUNT + 1;
3. Return;
-----------------------------------------------------------------
REMOVE-ITEM( QUEUE, FRONT, REAR, COUNT, ITEM)
This algorithm is used to remove or delete item
from circular queue.
1. If ( COUNT = 0 ) then
a. Display “Queue underflow”;
b. Return;
2. Otherwise
a. ITEM := QUEUE(FRONT)l
b. If ( FRONT =MAX ) then
i. FRONT := 1;
c. Otherwise
i. FRONT := FRONT + 1;
d. COUNT := COUNT + 1;
3. Return;
-----------------------------------------------------------------
EMPTY-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,EMPTY)
This is used to check queue is empty or not.
1. If( COUNT = 0 ) then
a. EMPTY := true;
2. Otherwise
a. EMPTY := false;
3. Return ;
-----------------------------------------------------------------
FULL-CHECK(QUEUE,FRONT,REAR,MAX,COUNT,FULL)
This algorithm is used to check queue is full or not.
1. If ( COUNT = MAX ) then
a. FULL := true;
2. Otherwise
a. FULL := false;
3. Return ;
#include <iostream>
using namespace std;
#define max 6
int queue[max]; // array declaration
int front = -1;
int rear = -1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if (front == -1 && rear == -1) // condition to check queue is empty
{
front = 0;
rear = 0;
queue[rear] = element;
}
else if ((rear + 1) % max == front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear = (rear + 1) % max; // rear is incremented
queue[rear] = element; // assigning a value to the queue at the rear position.
}
}
// function to delete the element from the queue
int dequeue()
{
if ((front == -1) && (rear == -1)) // condition to check queue is empty
{
cout << "\nQueue is underflow..";
}
else if (front == rear)
{
cout << "\nThe dequeued element is " << queue[front];
front = -1;
rear = -1;
}
else
{
cout << "\nThe dequeued element is " << queue[front];
front = (front + 1) % max;
}
}
// function to display the elements of a queue
void display()
{
int i = front;
if (front == -1 && rear == -1)
{
cout << "\n Queue is empty..";
}
else
{
cout << "\nElements in a Queue are :";
while (i <= rear)
{
cout << queue[i] << " ";
i = (i + 1) % max;
}
}
}
int main()
{
int choice = 1, x;
while (choice < 4 && choice != 0) // while loop
{
cout << "\n Press 1: Insert an element";
cout << "\nPress 2: Delete an element";
cout << "\nPress 3: Display the element";
cout << "\nEnter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
printf("Enter the element : ");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}
}
return 0;
}
Output :
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice : 1
Enter the element : 50
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice : 1
Enter the element : 60
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice : 3
Elements in a Queue are :50 60
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice : 2
The dequeued element is 50
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice : 3
0 Comments