Data Structure: Implementing Circular Queue in C++


Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is connected back to the first node to make a circle. Circular linked list fallow the First In First Out principle. Elements are added at the rear end and the elements are deleted at front end of the queue.

This C++ Program demonstrates the implementation of Circular Queue.
Here is source code of the C++ Program to demonstrate the implementation of Circular Queue. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C++ Program to Implement Circular Queue
 */
#include < iostream >
#define MAX 5
using namespace std;
/*
 * Class Circular Queue
 */
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 Overflow \n";
                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 del()
        {
            if (front == -1)
            {
                cout << "Queue Underflow\n";
                return ;
            }
            cout << "Element deleted 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;
            }
        }
        /*
         * Display Circular Queue
         */
        void display()
        {
            int front_pos = front, rear_pos = rear;
            if (front == -1)
            {
                cout << "Queue is empty\n";
                return;
            }
            cout << "Queue elements :\n";
            if (front_pos <= rear_pos)
            {
                while (front_pos <= rear_pos)
                {
                    cout<> choice;
        switch(choice)
        {
        case 1:
            cout << "Input the element for insertion in queue : ";
            cin >> item;	
            cq.insert(item);
	    break;
	case 2:
            cq.del();
	    break;
        case 3:
            cq.display();
	    break;
	case 4:
	    break;
	default:
	    cout << "Wrong choice\n";
	}/*End of switch*/
    }
    while(choice != 4);
    return 0;
}

Output
$ g++ circular_queue.cpp
$ a.out
 
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 6
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 4
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
3  2  6  4  1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Element deleted from queue is : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
2  6  4  1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4
 
------------------
(program exited with code: 1)
Press return to continue

0 comments:

Post a Comment

+