Implement Stack Using Two Queues in C++



Queue

The queue is a linear data structure that follows the First-In-First-Out (FIFO) operation. Where insertions are done at one end (rear) and deletions are done from another end (front). The first element that is entered is deleted first.

Following are the stack operations:

  • EnQueue (int data): Insertion at rear end
  • int DeQueue(): Deletion from front end

Stack

The stack is also a linear data structure that follows the Last-In-First-Out (LIFO) operation. Where the element will be added and removed from the top.

Following are the stack operations:

  • push (int data): Insertion at top
  • int pop(): Deletion from top

Algorithm to Implement Stack Using Two Queues

This pseudocode describes how to implement two separate queues (qu1 and qu2) using linked lists, with functions to enqueue (insert) and dequeue (delete) elements.

Begin
   function enqueue1 to insert item a at qu1:
   Set, np1 = new qu1
   np1->d1 = a
   np1->n1 = NULL
   if (f1 == NULL)
      Then set
      r1 = np1
      r1->n1 = NULL
      f1 = r1
   else
      r1->n1 = np1
      r1 = np1
      r1->n1 = NULL
End

Begin
   function dequeue1 to delete item from qu1.
   if queue is null
      Print no elements present in queue.
   Else
      q1 = f1
      f1 = f1->n1
      a = q1->d1
      delete(q1)
   return a
End

Begin
   function enqueue2 to insert item a at qu2.
   np2 = new qu2;
   np2->d2 = a;
   np2->n2 = NULL;
   if queue is null
      Set r2 = np2
      r2->n2 = NULL
      f2 = r2
   Else
      Set r2->n2 = np2
      r2 = np2
      r2->n2 = NULL
End

Begin
   function dequeue2 to delete item from qu2:
   if queue is null
      Print no elements present in queue.
   Else
      q2 = f2
      f2 = f2->n2
      a = q2->d2
      delete(q2)
   return a
End

C++ Program to Implement Stack Using Two Queues

The following C++ example implements a stack using two queues. It alternates between two linked list-based queues and reverses order during pop to maintain the Last-In-First-Out (LIFO) behavior.

#include<iostream>
using namespace std;
struct qu1 {
   qu1 * n1;
   int d1;
}* f1 = NULL, * r1 = NULL;

struct qu2 {
   qu2 * n2;
   int d2;
}* f2 = NULL, * r2 = NULL;

void enqueue1(int a) {
   qu1 * np1 = new qu1;
   np1 -> d1 = a;
   np1 -> n1 = NULL;
   if (f1 == NULL) {
      f1 = r1 = np1;
   } else {
      r1 -> n1 = np1;
      r1 = np1;
   }
}

int dequeue1() {
   if (f1 == NULL) return -1;
   int a = f1 -> d1;
   qu1 * temp = f1;
   f1 = f1 -> n1;
   delete temp;
   if (f1 == NULL) r1 = NULL;
   return a;
}

void enqueue2(int a) {
   qu2 * np2 = new qu2;
   np2 -> d2 = a;
   np2 -> n2 = NULL;
   if (f2 == NULL) {
      f2 = r2 = np2;
   } else {
      r2 -> n2 = np2;
      r2 = np2;
   }
}

int dequeue2() {
   if (f2 == NULL) return -1;
   int a = f2 -> d2;
   qu2 * temp = f2;
   f2 = f2 -> n2;
   delete temp;
   if (f2 == NULL) r2 = NULL;
   return a;
}

int main() {
   // Static array to simulate input
   int elements[] = {10, 20, 30, 40};
   int size = sizeof(elements) / sizeof(elements[0]);

   // Push elements into stack (via queue1)
   for (int i = 0; i < size; i++) {
      enqueue1(elements[i]);
   }

   // Pop all elements in LIFO order
   cout << "\nElements popped (LIFO order):\n";
   while (f1 != NULL || f2 != NULL) {
      if (f2 == NULL) {
         while (f1 && f1 -> n1 != NULL) {
            enqueue2(dequeue1());
         }
         cout << dequeue1() << endl;
      } else if (f1 == NULL) {
         while (f2 && f2 -> n2 != NULL) {
            enqueue1(dequeue2());
         }
         cout << dequeue2() << endl;
      }
   }
   return 0;
}

The above code generated the following output:

Elements popped (LIFO order):
40
30
20
10
Updated on: 2025-05-16T17:12:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements