Circular buffer example in C++

C

Circular buffer or circular queue is an improved version of the normal queue where the last index and the tail index are linked in a circular structure. Circular buffer in C++ follows two methods: enqueue() and dequeue(). We perform circular buffer or circular queue operations based on these methods.

  • The enqueue() method checks to see if the buffer is full. Otherwise, verify that the last index is the last one. If yes, set the tail value to 0. If not, increase the tail value by the value of that index.
  • The dequeue() function takes the value from the front index in the circular queue. If the queue is empty, a message will display that the queue is empty. Otherwise, it gets the last value and removes it from the queue.

Program to implement a circular buffer in C++

Following the two methods mentioned, we implement circular buffers in C++. Let's consider all the steps of circular queue implementation in C++.

#Involved<बिट्स/stdc++.h>

using namespace std;

Structure MyQueue

,

int here Head, tail,

int QSize;

int here ,NewArr;

MyQueue ,int no,,

Head , tail , -1,

Qsize = sz;

NewArr = new int[sz],

,

zero queue ,int val,,

int deQueue ,,,

zero show ,,,

,,

Starting with the code, we first create a “MyQueue” structure to initialize the head and tail variables. The head variable represents the front indices and the tail represents the back indices of an array. After that, the size of the circular queue, represented by the variable “Qsize”, is defined.

Then, we define a dynamically allocated array of “newArrays” that stores the values ​​of the circular queue. Next, we call MyQueue() which is a constructor and pass the parameter “sz” for the circular queue size. Inside the MyQueue() constructor, we assign the value “-1” to the head and tail pointers. This negative value indicates that the queue is now empty. Moving on, we specify the value “sz” which represents the size of the circular queue. The “NewArr” circular queue is set with a new keyword to create an array of integers within the specified “sz” size.

Then, we define the enQueue() and dequeue() functions. enqueue() Inserts values ​​into the circular queue defined by tail. However, the elements at the top of the circular queue are removed by the dequeue() function. The showQueue() member function displays the values ​​of the circular queue.

Step 1: Create a function to insert elements into a circular buffer

In the first step, we defined a class where private members are initialized and private member functions are set to implement the circular queue. Now, we set the function to create a circular queue and insert the values ​​inside the circular queue using the algorithm.

void MyQueue::enQueue ,int val,

,

If ,,Head , 0 , tail == QSize – 1, , ,tail , ,Head , 1, , ,QSize – 1,,,

,

court , ,\nThe queue is full”,

return,

,

Other If ,Head ,1,

,

Head , tail , 0,

NewArr[tail] =val;

,

Other If ,tail == QSize – 1 , Head ,, 0,

,

tail , 0,

NewArr[tail] =val;

,

Other,

tail,

NewArr[tail] =val;

,

,

Here, if the queue is empty or underflow then we call the function “enqueue()” from “MyQueue” class to insert the element in the circular queue. The “enqueue()” function is passed with the “val” parameter and the value is inserted from the tail of the circular queue. We set an “if-else” condition for it to insert values ​​into the circular queue. The first “if” statement which is “if ((head == 0 &&tail == Qsize – 1)) || (tail == (head – 1) % (Qsize – 1))” checks two conditions to see if The head is at the starting position and the tail is at the last position of the circular row. Then, it checks whether the tail is in the same position behind the head. If any of these conditions are met, the queue is not empty and generates the signal message.

Next, we have an “else-if” condition that identifies if the queue is empty or not. If so, the value is inserted into the queue. Since top is kept equal to -1, it indicates that the queue is empty and the value needs to be inserted into the circular queue. For this, we set the head and tail equal to 0. Then, we put the value from the tail position into the “newARR” circular queue.

Then, we have our third “else-if” condition that checks if the tail is at the last position of the queue and the head is not the starting position of the queue. This condition applies when the tail reaches the end and there is still space in the starting position. For this, we need to set the head to 0 and add the element to the tail position. Finally, if all the given conditions are not met, the queue is neither empty nor full. For this case, we increment the tail by 1 and the value is appended to the new tail position.

Step 2: Create a function to remove elements from the circular buffer

We have set up the previous code to create and insert elements into a circular queue using the enqueue() function. Now, we define the implementation of removing elements when the circular buffer overflows.

int MyQueue::deQueue ,,

,

If ,Head ,1,

,

court , ,\nThe queue is free”,

return INT_MIN;

,

int MyData = NewArr[head],

NewArr[head] , -1,

If ,Head , tail,

,

Head , -1,

tail , -1,

,

Other If ,Head == QSize – 1,

Head , 0,

Other

Head,

return My Information;

,

In the given code, we call dequeue() function from “Myqueue” class to remove the element from the head index. So, we have an “if” statement that checks if the queue is empty or not. The top is set with the value “-1” which represents an empty queue. Generates the message that the queue is empty and then returns INT_MIN which is the minimum value constant for an int. The “if” statement determines whether the queue is empty or not. For this, we define the “MyData” variable and set the value of the element at the top of the queue. Then, we set the head to the -1 state which indicates that this value has been removed from the queue. After this we check whether head and tail are equal or not. If both are equal, we assign the value “-1” to both, which represents an empty circular queue. Finally, we check whether dequeue() works if the head is at the last index of the queue. For this, we set it with a value of “0” which rotates to the beginning of the array. If any of the given conditions is not true, the value of top is incremented and the row element is returned.

Step 3: Create a function to show the elements of the circular buffer

In this section, we call the showQueue() function to display the elements of the “NewArr” circular queue.

void MyQueue::showQueue ,,

,

If ,Head ,1,

,

court , ,\nThe queue is free”,

return,

,

court , ,\nCircular queue element: “,

If ,tail ,, Head,

,

For ,integer i = Head, I <, tail, i++,

court,NewArr[i],,,

,

Other

,

For ,integer i = Head, I < QSize; i++,

court,NewArr[i],,,

For ,integer i = 0, I <, tail, i++,

court,NewArr[i],,,

,

,

The empty state of the queue is verified first. If the queue is free a prompt is displayed indicating that the circular queue is free. Otherwise, the function will show the elements of the circular queue. For this, we define an “if” statement where we have the tail which is greater than or equal to the head. This condition is set to handle the situation when the circular queue is not complete.

For this case, we use a “for” loop to iterate from head to tail and print the values ​​of the circular queue. The next case is where the circular queue is complete. For this, we check using the “if” condition where the tail is smaller than the head. Then, we need to use two loops where the first iterates from the head to the end of the queue and the second iterates from the beginning of the tail.

Step 4: Create the main() function of the circular queue program

Finally, we create the main() function of the program where we put five integers into the circular queue and display the integers of the queue. After that, we show the integers removed from the circular queue by calling the dequeue() function. After removing some elements, we refill the queue by inserting new elements using the enqueue() function.

int main ,,

,

MyQueue That ,5,,

, insert element In circular queue

queue.enQueue ,11,,

queue.enQueue ,12,,

queue.enQueue ,13,,

queue.enQueue ,14,,

queue.enQueue ,15,,

, display existing elements In circular queue

Q.ShoQ ,,,

, Removing elements from circular queue

court , ,\nRemoved element = “ , Q.DQ ,,,

court , ,\nRemoved element = “ , Q.DQ ,,,

Q.ShoQ ,,,

queue.enQueue ,16,,

queue.enQueue ,17,,

queue.enQueue ,18,,

Q.ShoQ ,,,

return 0,

,

Output:

The results of the circular queue implementation are shown on the C++ prompt screen.

conclusion

Finally, this article explains the circular buffer topic in depth. We first created a circular buffer, then explained how to remove from a circular queue, and then demonstrated the elements of a circular buffer in C++.

Add comment

By Ranjan