C Program for Reverse a Queue

reverse in a queue

How to Reverse a Queue in C programming language?

In this we will learn how to write a C Program for Reverse a Queue. In below sections, we will learn steps and code to perform the task.
For example :- Input : 10 12 3 14
Output : 14 3 12 10 (after reverse the queue)

Reverse a Queue in C

Method 1 : Reverse A Queue Using Stack

  1. Create a queue and a stack data structure. We’ll use the queue to store the input elements, and the stack to temporarily hold the elements while we reverse the queue.
  2. Enqueue all the elements of the original queue into the stack until the queue is empty.
  3. Dequeue all the elements from the stack and enqueue them back into the original queue.
  4. Repeat steps 2 and 3 until the stack is empty.
  5. The original queue will now be reversed.

Method 2 : Reverse A Queue Using Recursion

  1. Pop the front element from the queue and store it in a variable.
  2. Recursively reverse the remaining elements of the queue.
  3. Enqueue the front element that was popped in step 1 to the back of the reversed queue.
  4. Repeat steps 1 to 3 until all elements have been dequeued and enqueued.
  5. The original queue will now be reversed.

C Program for Reversing a Queue:-