Site icon T4Tutorials.com

Program to Implement Circular Queue in Data Structures (C plus plus)

Program to Implement Circular Queue in Data Structures (C plus plus)

In this tutorial, we will learn about Program to Implement Circular Queue in Data Structures (C plus plus).

/*C++ Program to Implement Circular Queue*/

#include <iostream>
#define MAX 5

using namespace std;

class circular_queue
{
    private:
        int *cqueue_arr;
        int front, rear;
        
    public:
        circular_queue()
        {
            cqueue_arr = new int [MAX];
            rear =  front = -1;
        }
        
        /*Insert into Circular Queue*/
        
        void insert(int item)
        {
            if ((front == 0 && rear == MAX-1) || (front == rear+1))
            {
                cout<<"Queue overflowed "<<endl;
                return;
            }
            
            if (front == -1)
            {
                front = 0;
                rear = 0;
            }
            
            else
            {
                if (rear == MAX - 1)
                    rear = 0;
                    
                else
                    rear = rear + 1;
            }
            
            cqueue_arr[rear] = item ;
        }
        
        /*Delete from Circular Queue*/
        
        void delet()
        {
            if (front == -1)
            {
                cout<<"Queue underflowed "<<endl;
                return;
            }
            
            cout<<"Value removed from queue is : "<<cqueue_arr[front]<<endl;
            if (front == rear)
            {
                front = -1;
                rear = -1;
            }
            
            else
            {
                if (front == MAX - 1)
                    front = 0;
                else
                    front = front + 1;
            }
        }
        
        /*Show Circular Queue*/
        
        void show()
        {
            int front_pos = front, rear_pos = rear;
            if (front == -1)
            {
                cout<<endl<<"Empty queue"<<endl;
                return;
            }
            
            cout<<endl<<"Values in queue are :"<<endl;
            
            if (front_pos <= rear_pos)
            {
                while (front_pos <= rear_pos)
                {
                    cout<<cqueue_arr[front_pos]<<"  ";
                    front_pos++;
                 }
            }
            
            else
            {
                while (front_pos <= MAX - 1)
                {
                    cout<<cqueue_arr[front_pos]<<"  ";
                    front_pos++;
                }
                front_pos = 0;
                while (front_pos <= rear_pos)
                {
                    cout<<cqueue_arr[front_pos]<<"  ";
                    front_pos++;
                }
            }
            cout<<endl;
        }
};


int main()
{
    int ch, value;
    circular_queue cq;
    do
    {
        cout<<"1. Insert into the queue "<<endl;
	    cout<<"2. Delete from the queue "<<endl ;
        cout<<"3. Display queue "<<endl;
        cout<<"4. Quit "<<endl;
        
        cout<<"Please enter your choice : ";
        cin>>ch;
        
        switch(ch)
        {
        case 1:
            cout<<"Please enter the value  for insertion in queue : ";
            cin>>value;	
            cq.insert(value);
	    break;
	case 2:
            cq.delet();
	    break;
        case 3:
            cq.show();
	    break;
	case 4:
	    break;
	default:
	    cout<<"Please enter a valid option"<<endl;
	}
    }
    
    while(ch != 4);
    return 0;
}

Output:

Program to Implement Circular Queue in Data Structures (C plus plus)
Exit mobile version