-->

Implementing Stack Using Queue

Posted by Admin on
What is the minimum number of Stack will be required to implement Queue Using Stack. Write a program to Implement Queue using Stack ? 

Two Stack minimum will be needed to implement Queue . There are two ways to implement it . in one way remove operation of Queue will optimized and in other operation insert operation of Queue will be optimized.   

Before the solution of this question i want you to see this post.

Method 1: (Insert Operation Optimized) 

Algorithm:
We have two stack assuming their name as s1 and s2
In the remove operation we  do the following :
    1. first we remove all the elements form the s1 except the last element 
       and add them in the s2 .
    2. Instead of adding the last element in s2 we will print it and delete.
    3. Then we wil again remove all the elements from the s2 and add them
       back in the s1.
In the insert operation we just simply add the element at the top of s1.

Implementation:
import java.util.Stack;
 
public class QueueUsingStack {
 
    // Stack class to make queue 
    Stack<Integer> s1 = new Stack<Integer>();
    Stack<Integer> s2 = new Stack<Integer>();
 
    // remove operation as explained in the algorithm above
    public int remove() {
        if (s1.peek() == null) {
            System.out.println("The Queue is empty, nothing to return");
            int i = 0;
            return i;
        } else {
                int num = 0;    
                for(int i=s1.size();i>0;i--)
                {
                        if(i==1)
                        num=s1.pop();
                        else
                        s2.push(s1.pop());
                }
        for(int i=s2.size();i>0;i--)
        {
                s1.push(s2.pop());
        }
        return num;              
        }
        
    }
 
    // insert method as explained in the algorithm above 
     public void insert(int data) {
 
        s1.push(data);
    }
 
    public static void main(String[] args) {
        QueueUsingStack q1 = new QueueUsingStack();
        q1.insert(1);
        q1.insert(2);
        q1.insert(3);
        q1.insert(4);
        q1.insert(5);
                
        System.out.println("1st element in Queue = " + q1.remove());
        System.out.println("2nd element in Queue = " + q1.remove());
        System.out.println("3rd element in Queue= " + q1.remove());
        System.out.println("4th element in Queue= " + q1.remove());
        System.out.println("5th element in Queue= " + q1.remove());
        
    }
}


Method 2: (Remove Operation Optimized)
Algorithm: 
We have two Stack assuming their name as s1 and s2
In the insert operation we  do the following :
    1.first we remove all the elements form the s1 and add them in the s2 .
    2.Then we will add the new element in the s1 .
    3.Then we wil again remove all the elements from the s2 and 
      add them back in the s1.
In the remove operation we just simply pop the element at the top of s1.

Implementation:
import java.util.Stack;
 
public class QueueImplementationUsingStack {
 
    // Stack class to make queue 
    Stack<Integer> s1 = new Stack<Integer>();
    Stack<Integer> s2 = new Stack<Integer>();
 
    // pop operation as exaplained above 
    public int remove() {
        if (s1.peek() == null) {
            System.out.println("The stack is empty, nothing to return");
            int i = 0;
            return i;
        } else {
            int pop = s1.pop();
            return pop;
        }
    }
 
    // push operation as explained above
     public void insert(int data) {
            for (int i = s1.size(); i > 0; i--) {
                s2.push(s1.pop());
            }
            s1.push(data);
            for (int j = s2.size(); j > 0; j--) {
                s1.push(s2.pop());
            }
    }
 
    public static void main(String[] args) {
        QueueImplementationUsingStack q1 = new QueueImplementationUsingStack();
        q1.insert(1);
        q1.insert(2);
        q1.insert(3);
        q1.insert(4);
        q1.insert(5);
                
        System.out.println("1st element in Queue = " + q1.remove());
        System.out.println("2nd element in Queue = " + q1.remove());
        System.out.println("3rd element in Queue= " + q1.remove());
        System.out.println("4th element in Queue= " + q1.remove());
        System.out.println("5th element in Queue= " + q1.remove());
        
    }
}

Please comment if you find any mistake.


No comments:

Post a Comment