Implement dynamic queue using templates class and a circular array Last Updated : 17 Aug, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to create a dynamic circular queue using a circular array having the following functionality: Front(): Get the front item from the queue.Back(): Get the last item from the queue.Push(X): Push the X in the queue at the end of the queue.Pop(): Delete an element from the queue. Below is the step-by-step illustration: Initially, the queue is empty. Insert element 1 to the back of the queue. Insert elements 2, 3, 4 to the back of the queue. Insert elements 5 to the back of the queue. Pop 4 elements from the queue. Pop 1 element from the queue. Approach: The idea is to double the size of the array used every time the capacity of the array gets full and copy the elements of the previous array into the new array. Follow the steps below to solve the problem: Initialize 4 variables say frontIndex, backIndex, sizeVar, and capacity and an array say arr[] to implement the queue,Define a function say Capacity() to find the size of the current array used:Return the capacity.Define a function say size() to find the count of elements in the queue:Return the variable sizeVar.Define a function say full() to find if the queue is full or not:Return true if sizeVar is equal to capacity. Otherwise, return false.Define a function say empty() to find if the queue is empty or not:If frontIndex and backIndex are equal to -1 then return true. Otherwise, return false.Define a function say Front() to print the front element of the queue:Print the element of arr[frontIndex] if queue is not empty().Define a function say Back() to print the last element of the queue:Print the element of arr[BackIndex] if queue is not empty().Define a function say Push(X) to insert an element at the end of the queue:If the queue is full then double the size of the current array and copy the elements of the previous array into the new array.If queue is empty() then assign frontIndex = backIndex = 0 and then assign X to both arr[frontIndex] and arr[backIndex] and then increment sizeVar by one.Else, update backIndex as backIndex = (backIndex+1)%capacity and then assign X to arr[backIndex] and increment sizeVar by one.Define a function say Pop() to delete an element at the front of the queue:If the queue is empty print "Underflow".Else if sizeVar is equal to 1 then assign -1 to frontIndex and backIndex both and then decrement sizeVar by one.Else, Update frontIndex as frontIndex = (frontIndex+1)%capacity and decrement sizeVar by one. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Class definition for queue template <class X> class Queue { private: // Stores the frontIndex int frontIndex; // Stores the back Index int backIndex; // Stores the array X* arr; // Stores the sizeof queue int sizeVar; // Stores the size of array int capacityVar = 4; public: // Queue class constructor Queue() { arr = new X[capacityVar]; frontIndex = backIndex = -1; sizeVar = 0; } // Function Methods bool empty(); bool full(); void push(X x); void pop(); X front(); X back(); int capacity(); int size(); }; // Find the capacity of queue template <class X> int Queue<X>::capacity() { return capacityVar; } // Find the number of elements // present in Queue template <class X> int Queue<X>::size() { return sizeVar; } // Function to check if // Queue is empty or not template <class X> bool Queue<X>::empty() { if (frontIndex == -1 && backIndex == -1) return true; else return false; } // Function to check if the queue // is full or not template <class X> bool Queue<X>::full() { if (sizeVar == capacityVar) return true; else return false; } // Function to find the front element // of the queue template <class X> X Queue<X>::front() { // If queue is empty if (empty()) { cout << "Queue underflow" << endl; abort(); } return arr[frontIndex]; } // Function to find the last element // of the Queue template <class X> X Queue<X>::back() { if (empty()) { cout << "Queue underflow" << endl; abort(); } return arr[backIndex]; } // Function to insert the element // to the rear end of the queue template <class X> void Queue<X>::push(X x) { if (full()) { // If the queue is full, then // double the capacity capacityVar = capacityVar * 2; // Initialize new array of // double size X* temp = new X[capacityVar]; // Copy the elements of the // previous array in the order of the queue for (int i = 0, j= frontIndex; i < sizeVar; i++){ temp[i] = arr[j]; j = (j+1) % sizeVar; } // update the front and rear indices in the new array frontIndex = 0; backIndex = sizeVar -1; // Deallocate the memory // of previous array delete[] arr; arr = temp; } // If size is zero if (empty()) { frontIndex = backIndex = 0; arr[backIndex] = x; sizeVar++; return; } // Increment the backIndex backIndex = (backIndex + 1) % capacityVar; arr[backIndex] = x; sizeVar++; return; } // Function to pop an element from // front end of the queue template <class X> void Queue<X>::pop() { // If queue is empty if (empty()) { cout << "Queue underflow" << endl; abort(); } // If there is only one character if (frontIndex == backIndex) { // Mark Queue as empty // and decrement sizeVar frontIndex = backIndex = -1; sizeVar--; return; } // Increment frontIndex cyclically // using modulo arithmetic frontIndex = (frontIndex + 1) % capacityVar; sizeVar--; return; } // Driver Code int main() { // Queue initialization Queue<int> q; // Iterate the range [1, 100] for (int i = 1; i < 5; i++) q.push(i); // Print the current capacity cout << "Current capacity " << q.capacity() << endl; // Print current size cout << "Current size " << q.size() << endl; // Print front elements of queue cout << "Front element " << q.front() << endl; // Print last element of the queue cout << "Rear element " << q.back() << endl; cout << endl; cout << "Pop an element" << endl; // Pop an element from the queue q.pop(); cout << "Pop an element" << endl; // Pop an element from the queue q.pop(); cout << endl; // Print the current capacity cout << "Current capacity " << q.capacity() << endl; // Print current size cout << "Current size " << q.size() << endl; // Print front elements of queue cout << "Front element " << q.front() << endl; // Print last element of the queue cout << "Rear element " << q.back() << endl; cout << endl; cout << "Push element 5" << endl; cout << "Push element 6" << endl; q.push(5); q.push(6); cout << endl; // Print the current capacity cout << "Current capacity " << q.capacity() << endl; // Print current size cout << "Current size " << q.size() << endl; // Print front elements of queue cout << "Front element " << q.front() << endl; // Print last element of the queue cout << "Rear element " << q.back() << endl; cout << endl; cout << "Push element 7" << endl; q.push(7); cout << endl; // Print the current capacity cout << "Current capacity " << q.capacity() << endl; // Print current size cout << "Current size " << q.size() << endl; // Print front elements of queue cout << "Front element " << q.front() << endl; // Print last element of the queue cout << "Rear element " << q.back() << endl; return 0; } Output: Current capacity 128 Current size 99 Front element 1 Rear element 99 Pop an element Pop an element Current capacity 128 Current size 97 Front element 3 Rear element 99 Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Implement dynamic queue using templates class and a circular array S shaadk7865 Follow Improve Article Tags : Queue DSA Practice Tags : Queue Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like