Tuesday, 17 November 2015

C++ program to Reverse a Queue.

/*
program to reverse the queue using stack. Just deque the queue and push each dequeued element into the stack. After this pop the elements of the stack and enqueue into the queue.
*/
#include<bits/stdc++.h>
using namespace std;
void show_content(queue<int> q)
{
    queue<int> temp_queue=q;
    cout<<"Contents of the Queue are:\n";
    if(q.size()==0)
    {
        cout<<"No elements in Queue\n";
    }
    else
    {
        while(!temp_queue.empty())
        {
            int d=temp_queue.front();
            temp_queue.pop();
            cout<<d<<" ";
        }
    }
    cout<<"\n";
}
int main()
{
    queue<int> q;
    q.push(23);
    q.push(13);
    q.push(54);
    q.push(85);
    q.push(71);
    q.push(59);
    q.push(44);
    q.push(42);
    q.push(12);
    q.push(17);
    q.push(92);
    q.push(94);
    q.push(59);
    q.push(66);
    show_content(q);
    stack<int> stk;
    while(!q.empty())
    {
        int data=q.front();
        q.pop();
        stk.push(data);
    }
    while(!stk.empty())
    {
        int data=stk.top();
        stk.pop();
        q.push(data);
    }
    show_content(q);
    return 0;
}


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;
}


C++ program to implement Stack using 2 Queues.

/*Idea is to take 2 queues namely q1 and q2 . Now to perform push() operation in a stack . Insert the element in the empty queue(let's say q1 is empty )and along with that pop() all the elements of the other queue q2 and push() them in queue q1. As queue q2 now become empty so pushing of new element will take place in queue q2 and process repeats. Below is the implementation of the above idea.
Below is the diagrammatic representation of the above idea.

Step 0:

"Stack"
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
|   |   |   |   |   |  |   |   |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Step 1:
"Stack"
+---+---+---+---+---+
| 1 |   |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
| 1 |   |   |   |   |  |   |   |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Step 2:

"Stack"
+---+---+---+---+---+
| 2 | 1 |   |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
|   |   |   |   |   |  | 2 | 1 |   |   |   |
+---+---+---+---+---+  +---+---+---+---+---+
Step 3:
"Stack"
+---+---+---+---+---+
| 3 | 2 | 1 |   |   |
+---+---+---+---+---+

Queue A                Queue B
+---+---+---+---+---+  +---+---+---+---+---+
| 3 | 2 | 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> &q1,queue<int> &q2)
{
    if(q1.empty())
    {
        q1.push(element);
        while(!q2.empty())
        {
            int value=q2.front();
            q2.pop();
            q1.push(value);
        }
        show_content(q1);
    }
    else
    {
        if(q2.empty())
        {
            q2.push(element);
            while(!q1.empty())
            {
                int value=q1.front();
                q1.pop();
                q2.push(value);
            }
            show_content(q2);
        }
    }
}
void Pop(queue<int> &q1,queue<int> &q2)
{
    if(!q1.empty()){
        q1.pop();
        show_content(q1);
    }
    else{
        q2.pop();
        show_content(q2);
    }
}
int main()
{
    int a,b,c;
    queue<int> q1;
    queue<int> q2;
    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,q1,q2);
            break;
        case 2:
            Pop(q1,q2);
            break;
        case 3:
            cout<<"\n";
            return 0;
            break;
        }
    }while(1);
    return 0;
}