Tuesday, 17 November 2015

C++ program to implement Stack using 1 Queue.

/* Idea is to take a queue let's say Q . As initially queue will be empty ,now suppose we insert an element in the queue. Element will be at the front so no problem in popping that element as it maintains the property of stack. But what if we insert the next element , property of stack will be no more satisfied . In order to correct that we pop the n-1 elements from the queue and enque them.
Where n is the number of elements in the queue. With this the last inserted element will always be at the front of the queue satisfying the property of stack.
push 1
Below is the diagramatic representation.

front                     
+----+----+----+----+----+----+
| 1  |    |    |    |    |    |    insert 1
+----+----+----+----+----+----+

push2

front                     
+----+----+----+----+----+----+
| 1  | 2  |    |    |    |    |    insert 2
+----+----+----+----+----+----+

     front                     
+----+----+----+----+----+----+
|    | 2  |  1 |    |    |    |    remove and insert 1
+----+----+----+----+----+----+

 insert 3

      front                     
+----+----+----+----+----+----+
|    | 2  |  1 |  3 |    |    |    insert 3
+----+----+----+----+----+----+

           front                     
+----+----+----+----+----+----+
|    |    |  1 |  3 |  2 |    |    remove and insert 2
+----+----+----+----+----+----+

                front                     
+----+----+----+----+----+----+
|    |    |    |  3 |  2 |  1 |    remove and insert 1

+----+----+----+----+----+----+

Below is the implementation of the above idea.
*/

#include<bits/stdc++.h>
using namespace std;
void show_content(queue<int> q)
{
    queue<int> temp_queue=q;
    cout<<"Contents of the stack are:\n";
    if(q.size()==0)
    {
        cout<<"No elements in stack\n";
    }
    else
    {
        while(!temp_queue.empty()){
            int d=temp_queue.front();
            temp_queue.pop();
            cout<<" -\n|";
            cout<<d<<"|\n";
        }
    }
    cout<<"\n";
}
void Push(int element,queue<int> &q)
{
    if(q.empty()){
       q.push(element);
    }
    else{
        q.push(element);
        int sz=q.size();sz--;
        while(sz--){
            int data=q.front();
            q.pop();
            q.push(data);
        }
    }
    show_content(q);
}
void Pop(queue<int> &q)
{
    int data=q.front();
    q.pop();
    show_content(q);
}
int main()
{
    int a,b,c;
    queue<int> q;
    do
    {
        cout<<"Press 1 to push element\n";
        cout<<"Press 2 to pop element\n";
        cout<<"Press 3 to end operations\n";
        cin>>a;
        switch(a)
        {
        case 1:
            cout<<"Enter the element to be pushed in stack\n";
            cin>>b;
            Push(b,q);
            break;
        case 2:
            Pop(q);
            break;
        case 3:
            cout<<"\n";
            return 0;
            break;
        }
    }while(1);
    return 0;
}


1 comment:

  1. Play for real - Casino Secret
    Get a free $25 bonus and 300 カジノ シークレット free spins! Play online casino games with real money vua nhà cái bonuses, spins, cashbacks. Play for 메리트카지노 free now! | online.casino

    ReplyDelete