Reversing stack Method 2 !! (One Helper Stack only)

#include <bits/stdc++.h>
using namespace std;
stack<intrev(stack<ints)
{
    int size = s.size(), xyt;
    stack<ints2;
    size -= 1;
    while (size--)
    {
        t = size + 1;
        x = s.top();
        s.pop();
        while (t--)
        {
            s2.push(s.top());
            s.pop();
        }
        s.push(x);
        while (!s2.empty())
        {
            s.push(s2.top());
            s2.pop();
        }
        y -= 1;
    }
    return s;
}
int main()
{
    int nix;
    stack<intst;
    cout << "Enter the number of values : ";
    cin >> n;
    for (i = 0i < ni++)
    {
        cin >> x;
        s.push(x);
    }
    t = s;
    cout << "\nStack before reversing\nTop->> ";
    while (!t.empty())
    {
        cout << t.top() << " ";
        t.pop();
    }
    cout << " <<-Bottom\n";
    cout << "\nStack after reversing\nTop->> ";
    t = rev(s);
    while (!t.empty())
    {
        cout << t.top() << " ";
        t.pop();
    }
    cout << " <<-Bottom";
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree