×

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

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

3 minutes read.

Finding Rank in a Binary Search Tree

Implementation // writing a C++ program to find out the rank and element in the program.  #include <bits/stdc++.h> using namespace std; struct __nod { int record; __nod *Lft, *Rt; int LftSize; }; __nod* new__nod(int record) { __nod *temp = new __nod; temp->record...

6 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

Circular Queue

Circular Queue Circular Queue is special type queue, which follows First in First Out (FIFO) rule and as well as instead of ending queue at the last position, it starts again...

4 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Red Black Tree vs AVL Tree: Data Structure

Difference Between Red Black Tree vs AVL Tree Red Black Tree: A red-black tree is referred as self-balancing binary search tree. In red-black, each node stores an extra bit that determines...

4 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

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.