Implementation of Queue using two stacks in C

Queue using two Stack in C

Queue and stack are fundamentally two different linear data structures, but given with one or more of any one data structures, we can just convert it into other and apply it in real. Here, in this section we will learn, Implementation of Queue using two stacks in C . We will built enqueue operation , dequeue operation functions using stacks.
Implementation of Queue using two stacks in C

Implementation of Queue using two stacks in C

Algorithm :

  • We take two stacks say stack1 and stack2.
  • stack1 will be working as the main queue and stack2 will help us in reversing the order of stack1.
  • For enqueue operation , We will simply push the element let say x into stack1.
  • For dequeue operation ,
    • If both the stack1 and stack2 are empty then we need to print that queue is empty.
    • If stack2 is empty then we need to push all the elements from stack1 to stack2 .
    • Pop the element from stack2 and return it.
Implementation of Queue using two stacks in CImplementation of Queue using two stacks in C

C++ Program based on above algorithm :

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

#define N 100			// size for arrays representing stack1 and stack2

int stack1[N], stack2[N];	// array representing stacks of size N

int top_stack1 = -1;		// top for stack1
int top_stack2 = -1;		// top for stack2

int count = 0;			// For keeping the count of element present in queue

void push_stack1 (int data)
{
  if (top_stack1 == N - 1)
    {
      printf ("Stack1 is overflow");
      return;
    }

  else
    {
      top_stack1++;
      stack1[top_stack1] = data;
    }

  return;
}

void push_stack2 (int data)
{
  if (top_stack2 == N - 1)
    {
      printf ("Stack2 is overflow");
      return;
    }

  else
    {
      top_stack2++;
      stack2[top_stack2] = data;
    }

  return;

}

int pop_stack1 ()
{
  if (top_stack1 == -1)
    {
      printf ("Stack1 is underflow\n");
      return -1;
    }

  return stack1[top_stack1--];
}

int pop_stack2 ()
{

  if (top_stack2 == -1)
    {
      printf ("Stack2 is underflow\n");
      return -1;
    }

  return stack2[top_stack2--];

}

void enqueue (int data)
{
  push_stack1 (data);
  count++;

}

void dequeue ()
{
  if (top_stack1 == -1 && top_stack2 == -1)
    printf ("Queue is empty\n");

  else
    {
      for (int i = 0; i < count; i++)
	{

	  int temp = pop_stack1 ();
	  push_stack2 (temp);
	}

      int x = pop_stack2 ();

      printf ("Dequeued element is %d\n", x);
      count--;

      for (int i = 0; i < count; i++)
	{

	  int temp = pop_stack2 ();
	  push_stack1 (temp);

	}
    }
}

void display ()
{
  if (top_stack1 == -1)
    {
      printf ("Queue is empty \n");
      return;
    }

  for (int i = 0; i < top_stack1; i++)
    printf ("%d ", stack1[i]);

  printf ("\n");

}

void top ()
{
  printf ("Top element of queue is %d ", stack1[0]);
}

int main ()
{

  enqueue (3);
  enqueue (45);
  enqueue (-1);

  display ();

  dequeue ();

  display ();

  return 0;

}

Output

Elements in queue : 3 45 -1

Deleted element : 3

Elements in queue : 45 -1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java