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

Circular Queue

This C++ program implements a circular queue data structure using an array. It includes functions to check if the queue is empty or full, enqueue elements, dequeue elements, and get the front element. The main function uses a menu to allow the user to enqueue or dequeue elements and test the queue functionality.

Uploaded by

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

Circular Queue

This C++ program implements a circular queue data structure using an array. It includes functions to check if the queue is empty or full, enqueue elements, dequeue elements, and get the front element. The main function uses a menu to allow the user to enqueue or dequeue elements and test the queue functionality.

Uploaded by

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

#include<iostream>

using namespace std;


#include<iomanip>
#include<cstdlib>

#define SIZE 5

class queue
{
private:
int arr[SIZE];
int front;
int rear;
public:
queue()
{
front = -1;
rear = -1;
}
bool is_queue_empty(void)
{
return ( rear == -1 && front == rear );
}
bool is_queue_full(void)
{
return ( front == (rear+1)%SIZE );
}

void enqueue(int ele)


{
//- increment the value of rear by 1
rear = (rear+1)%SIZE;
//- insert ele into the queue at rear position
arr[ rear ] = ele;

//if front == -1 => make it is as 0


if ( front == -1 )
front = 0;
}

void dequeue(void)
{
if( front == rear )//we are deleting last ele
{
front = rear = -1;//reinitialize front and rear to -1
}
else
{
/*- increment the value of front by 1 -- i.e. delete ele from the
queue which
is at front end. */
front = (front + 1) % SIZE;
}
}

int get_front(void)
{
//return the value of ele which is at front end
return ( arr[ front ] );
}

};

enum menu_options{ EXIT, ENQUEUE, DEQUEUE };


int menu(void)
{
int choice;
cout << "circular queue" << endl;
cout << "0. exit" << endl;
cout << "1. enqueue" << endl;
cout << "2. dequeue" << endl;
cout << "enter the choice: ";
cin >> choice;
return choice;
}

int main(void)
{
int choice;
queue q;
int ele;

while(1)
{
choice = menu();
switch(choice)
{
case EXIT: exit(0);

case ENQUEUE:
//check queue is not full
if( !q.is_queue_full())
{
cout << "enter the ele: ";
cin >> ele;
q.enqueue(ele);
cout << ele << " inserted into the queue ..." << endl;
}
else
cout << "queue is full !!!" << endl;
break;

case DEQUEUE:
//- check queue is not empty
if( !q.is_queue_empty() )
{
ele = q.get_front();
q.dequeue();
cout << "deleted ele is: " << ele << endl;
}
else
cout << "queue is empty !!!" << endl;
break;
}
}
return 0;
}

You might also like