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:-

Run
#include<stdio.h> 
#include<stdlib.h> 

#define MAX_SIZE 100

struct Queue
{
  int items[MAX_SIZE];
  int front;
  int rear;
};

struct Stack
{
  int items[MAX_SIZE];
  int top;
};

void enqueue (struct Queue *q, int item)
{
  if (q->rear == MAX_SIZE - 1)
    {
      printf ("Queue overflow\n");
      return;
    }

  if (q->front == -1)
    {
      q->front = 0;
    }

  q->rear++;
  q->items[q->rear] = item;
}

int dequeue (struct Queue *q)
{
  if (q->front == -1)
    {
      printf ("Queue underflow\n");
      return -1;
    }

  int item = q->items[q->front];
  q->front++;

  if (q->front > q->rear)
    {
      q->front = q->rear = -1;
    }

  return item;
}

void display (struct Queue *q)
{
  if (q->rear == -1)
    {
      printf ("Queue is empty\n");
      return;
    }

  for (int i = q->front; i <= q->rear; i++)
    {
      printf ("%d ", q->items[i]);
    }
  printf ("\n");
}

void push (struct Stack *s, int item)
{
  if (s->top == MAX_SIZE - 1)
    {
      printf ("Stack overflow\n");
      return;
    }

  s->top++;
  s->items[s->top] = item;
}

int pop (struct Stack *s)
{
  if (s->top == -1)
    {
      printf ("Stack underflow\n");
      return -1;
    }

  int item = s->items[s->top];
  s->top--;

  return item;
}

int main ()
{
  struct Queue q;
  q.front = -1;
  q.rear = -1;

  struct Stack s;
  s.top = -1;

  enqueue (&q, 1);
  enqueue (&q, 2);
  enqueue (&q, 3);
  enqueue (&q, 4);

  printf ("Queue before reversing:\n");
  display (&q);

  while (q.front != -1)
    {
      push (&s, dequeue (&q));
    }

  while (s.top != -1)
    {
      enqueue (&q, pop (&s));
    }

  printf ("Queue after reversing:\n");
  display (&q);

  return 0;
}

Output

Queue before reversing:
1 2 3 4 
Queue after reversing:
4 3 2 1 
  • In this code, we define two data structures: Queue and Stack. The Queue is implemented using an array and has two pointers – front and rear. The Stack is also implemented using an array and has one pointer – top.
  • The enqueue() and dequeue() functions are used to insert and remove items from the queue, respectively. The display() function is used to display the contents of the queue.
  • The push() and pop() functions are used to insert and remove items from the stack, respectively.
  • To reverse the queue using a stack, we first dequeue all the items from the queue and push them onto the stack. Then, we pop all the items from the stack and enqueue them back into the queue. This effectively reverses the order of the items in the queue.