Program to Reverse a Queue - Code Explained

 

Today we will be discussing the Program to Reverse a Queue - Code Explained. But before reading further if you are unfamiliar with the concept of queue then refer to this link. Suppose we have a queue 1->2->3->4 then after reversing it should look like 4->3->2->1. Now let's discuss the procedure:


Procedure


1. First we will make a queue and insert some values in it (data structures types). 
2. Now make a function to reverse the queue. In that function we will iterate the list from the rear until we reach the front of the queue. 
3. This will print the queue in reverse order.

Program to Reverse a Queue - Code Explained








Code







Explanation of code

1. First we will write a simple code of queue which will have:
  • Queue() : It is a constructor which will initialize the size of array, front and rear.
  • empty() : This function checks if the queue is empty or not.
  • full() : This function checks if the queue is full or not.
  • enqueue() : This function inserts the data in the queue.
  • show() : This function prints the queue.
2. Insert some values in the queue. (data structures types)
3. Now we have created a function named reverse() to Program to Reverse a Queue - Code Explained .
4. In the reverse() we have written a for loop which starts from rear of the queue and prints the values of the queue until it reaches the front of the queue. 
    for(int i=rear;i>=front+1;i--)
    {
         cout<<a[i]<<" ";
    }
** This was the Program to Reverse a Queue - Code Explained. I hope you have understood the logic.


1 Comments

  1. Nice Explanation Man!!!✌️
    Keep posting articles like this...
    thanks

    ReplyDelete
Post a Comment
Previous Post Next Post