×

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

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

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.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

String Operations in Data Structures

Operations on Strings Reversing the order of words in a sentence Reversing a string is a technique that reverses or alters the order of a given string so that the last character...

9 minutes read.

Application of Stack in Data Structures

In this article, we will discuss all the different applications of stack. What is meant by stack? The stack is a non-primitive linear data structure in which the insertion of the new...

11 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

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.