×

Queue Implementation using stacks Data Structure

Queue Implementation using stacks

In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances of stack so we can perform operations on instances of stack only

 We can implement a queue using two stacks. so we can say ‘q’ be a queue which we want to implement and ‘s1’, ‘s2’ are the stacks to be used.

So, we can implement Queue using two way and they are given below:

Method 1 (By making enqueue operation costly)

In this method, we initialize two stacks, stack1 and  stack2 with Top1 = Top2 = -1.

Enqueue(s, x) operation’s step are described below:

  • We will push all the elements from stack1 to stack2.
  • Then we will push new element to stack2.
  • After we will pop all the elements from stack2 to stack1.

Dequeue(s) operation’s function are described below:

  • Pop and return the element from stack1.

Implementation: -

 #include <stdio.h>
 #include <stdlib.h>
 /* Functions and variables used */
 void push1(int);
 void push2(int);
 int pop1();
 int pop2();
 void enqueue();
 void dequeue();
 void display();
 void create();
 int stack1[100], stack2[100];
 int top1 = -1, top2 = -1;
 int count = 0;
 /* This is the Main Function */
 int main()
 {
 int choice;
 printf("\nQUEUE USING STACKS IMPLEMENTATION\n\n");
 printf("\n1. ENQUEUE ELEMENT INTO THE QUEUE ");
 printf("\n2. DEQUEUE ELEMENT FROM THE QUEUE ");
 printf("\n3. DISPLAY  ELEMENTS IN THE QUEUE ");
 printf("\n4. EXIT FROM THE PROGRAM ");
 printf("\n");
 create();
 while (1)
 {
 printf("\n Enter your choice : ");
 scanf("%d", &choice);
 switch (choice)
 {
 case 1:
 enqueue();
 break;
 case 2:
 dequeue();
 break;
 case 3:
 display();
 break;
 case 4:
 exit(0);
 default:
 printf("\n Invalid Choice \n");
 }
 }
 }
 /* Function to initialize top of two stacks*/
 void create()
 {
 top1 = top2 = -1;
 }
 /* Function to push an element to stack */
 void push1(int element)
 {
 stack1[++top1] = element; // Pushing the element to stack1
 }
 /* Function to pop element from the stack */
 int pop1()
 {
 return(stack1[top1--]); // Pop element from the stack1
 }
 /* Function to push an element on to the stack */
 void push2(int element)
 {
 stack2[++top2] = element; // Pushing the element to the stack2
 }
 /* Function to pop an element from the stack */
 int pop2()
 {
 return(stack2[top2--]); // pop element from the stack2
 }
 /* Function to enqueue an element into the queue using the stack */
 void enqueue()
 {
 int data, i;
 printf("Enter the data : ");
 scanf("%d", &data);
 push1(data); // Push data from stack to the queue
 count++;
 }
 /* Function to dequeue an element from the queue using stack */
 void dequeue()
 {
 int i;
 for (i = 0; i <= count; i++)
 {
 push2( pop1() ); // Pop elements from the stack1 and push them to the stack2
 }
 pop2(); // Pop the element from the stack2 which is the element to be dequeued
 count--;
 for (i = 0; i <= count; i++)
 {
 push1(pop2()); //  Push back all the elements from the stack2 to the stack1
 }
 }
 /*Function to display the elements in the queue*/
 void display()
 {
 int i;
 if(top1 == -1)
 {
 printf("\nEMPTY QUEUE\n");
 }
 else
 {
 printf("\nQUEUE ELEMENTS ARE :  ");
 for (i = 0; i <= top1; i++)
 {
 printf(" %d ", stack1[i]);
 }
 printf("\n");
 }
 } 

Output: -

Queue Implementation using stacks Data Structure

Method 2 (By making dequeue operation costly)

In this method, we initialize two stacks, stack1 and stack2 with Top1 = Top2 = -1.

Enqueue(s, x) operation’s step are described below:

  • We will push the element into the stack1.

Dequeue(s) operation’s function are described below:

  • We will push all the element from stack1 to stack2.
  • Pop and return the element from stack2.

Implementation: -

 #include <stdio.h>
 #include <stdlib.h>
 /* Functions and variables used */
 void push1(int);
 void push2(int);
 int pop1();
 int pop2();
 void enqueue();
 void dequeue();
 void display();
 void create();
 int stack1[100], stack2[100];
 int top1 = -1, top2 = -1;
 int count = 0;
 /* This is the Main Function */
 int main()
 {
 int choice;
 printf("\n QUEUE USING STACKS IMPLEMENTATION\n\n ");
 printf("\n1. ENQUEUE ELEMENT INTO THE QUEUE ");
 printf("\n2. DEQUEUE ELEMENT FROM THE QUEUE");
 printf("\n3. DISPLAY  ELEMENTS IN THE QUEUE");
 printf("\n4. EXIT FROM THE PROGRAM");
 printf("\n");
 create();
 while (1)
 {
 printf("\nEnter your choice : ");
 scanf("%d", &choice);
 switch (choice)
 {
 case 1:
 enqueue();
 break;
 case 2:
 dequeue();
 break;
 case 3:
 display();
 break;
 case 4:
 exit(0);
 default:
 printf("\nInvalid Choice\n");
 }
 }
 }
 /* Function to initialize top of two stacks*/
 void create()
 {
 top1 =  top2 = -1;
 }
 /* Function to push an element to stack */
 void push1(int element)
 {
 stack1[++top1] = element; // Pushing the element to the stack1
 }
 /* Function to pop element from stack */
 int pop1()
 {
 return(stack1[top1--]); // Pop element from stack1
 }
 /* Function to push an element on to stack */
 void push2(int element)
 {
 stack2[++top2] = element; // Pushing the element to stack2
 }
 /* Function to pop an element from stack */
 int pop2()
 {
 return(stack2[top2--]); // pop element from stack2
 }
 /* Function to enqueue an element into the queue using stack */
 void enqueue()
 {
 int data, i;
 printf("Enter the data : ");
 scanf("%d", &data);
 push1(data); // Push data from stack to the queue
 count++;
 }
 /* Function to dequeue an element from the queue using stack */
 void dequeue()
 {
 int i;
 for (i = 0; i <= count; i++)
 {
 push2(pop1()); // Pop elements from stack1 and push them to stack2
 }
 pop2(); // Pop the element from stack2 which is the element to be dequeued
 count--;
 for (i = 0; i <= count; i++)
 {
 push1(pop2()); // Push back all the elements from stack2 to stack1
 }
 }
 /*Function to display the elements in the queue*/
 void display()
 {
 int i;
 if(top1 == -1)
 {
 printf("\nEMPTY QUEUE\n");
 }
 else
 {
 printf("\nQUEUE ELEMENTS :  ");
 for (i = 0; i <= top1; i++)
 {
 printf(" %d  ", stack1[i]);
 }
 printf("\n");
 }
 } 

Output: -

Queue Implementation using stacks Data Structure

Related Topics

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Preorder Traversal of Binary Trees

In general, Stack, Array, Queue, and other linear data structures only have one way to traverse the data. However, there are numerous ways to traverse through the data in a hierarchical...

3 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

4 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.