Representing Queue using 2 stacks


#include<iostream>
#include<stack>
using namespace std;

int main()
{
    int arr[]={1,4,6,8,2,5,10,12,14};
    stack<int> st1;
    stack<int> st2;
    for(int i=0;i<9;i++)
    {
        while(!st1.empty())
        {
            st2.push(st1.top());
            st1.pop();
        }
        st1.push(arr[i]);
        while(!st2.empty())
        {
            st1.push(st2.top());
            st2.pop();
        }
    }
    for(int i=0;i<9;i++)
    {
        cout<<st1.top()<<" ";
        st1.pop();
    }
    return 0;
}

Comments

Popular Posts