Circular Queue using Array in C++
Circular Queue in C++
A basic linear data structure is Linear queue, where you can get First in First out feature for nodes. Circular Queue is a implementation of that very linear queue in which you can overcome the problems regarding linear fixed length queues. Here is an article on how to implement a Circular Queue using array in C++.
Queue VS Circular Queue?
Members of Circular Queue:
- Front : From where nodes are to be popped out.
- Back : Back or Rear, where nodes are pushed into.
- Enqueue : Pushing a new node at the back.
- Dequeue : Popping out nodes from front, if any.
- Overflow : If the Circular Queue has predefined storage and is full.
- Underflow : If the Circular Queue is empty.
- Size : Number of nodes present.
If we create a queue using an array, where enqueuing and dequeuing takes O(1) time and space complexity each, one the queue is full, you cannot insert new elements, also sometimes when you delete elements, the queue may be totally empty but still the front will be in the back most position, so you can’t insert any value over there.
In this meantime, a circular queue comes into the picture. In Circular queue, the front and back only overlap when the queue is empty, but there is no problem when you want to delete elements and insert them again. The front pointer wanders around the queue in the circular path.
Also : Circular Queue using Array
C++ Program for the implementation of circular queue using Array
//Circular queue C++ program #include<iostream> #define MAX_SIZE 100 // Maximum size of the queue using namespace std; class CircularQueue { private: int front, rear; int arr[MAX_SIZE]; public: CircularQueue () { front = -1; rear = -1; } // Function to check if the queue is full bool isFull () { if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1) % (MAX_SIZE - 1))) { return true; } return false; } // Function to check if the queue is empty bool isEmpty () { if (front == -1) { return true; } return false; } // Function to add an element to the queue void enQueue (int value) { if (isFull ()) { cout << "Queue is full." << endl; } else { if (front == -1) { front = 0; } rear = (rear + 1) % MAX_SIZE; arr[rear] = value; cout << "Enqueued element: " << value << endl; } } // Function to remove an element from the queue int deQueue () { int element; if (isEmpty ()) { cout << "Queue is empty." << endl; return -1; } else { element = arr[front]; if (front == rear) { front = -1; rear = -1; } else { front = (front + 1) % MAX_SIZE; } cout << "Dequeued element: " << element << endl; return element; } } // Function to display the elements in the queue void display () { if (isEmpty ()) { cout << "Queue is empty." << endl; } else { cout << "Elements in the queue: "; int i; for (i = front; i != rear; i = (i + 1) % MAX_SIZE) { cout << arr[i] << " "; } cout << arr[i] << endl; } } }; int main () { CircularQueue q; q.enQueue (10); q.enQueue (20); q.enQueue (30); q.enQueue (40); q.display (); q.deQueue (); q.deQueue (); q.display (); q.enQueue (50); q.enQueue (60); q.display (); return 0; }
Output: Enqueued element: 10 Enqueued element: 20 Enqueued element: 30 Enqueued element: 40 Elements in the queue: 10 20 30 40 Dequeued element: 10 Dequeued element: 20 Elements in the queue: 30 40 Enqueued element: 50 Enqueued element: 60 Elements in the queue: 30 40 50 60
Login/Signup to comment