Write a function reverseStack, which takes a stack object as input parameter and uses a queue object to reverse the elements of the stack.

Write a function reverseStack, which takes a stack object as input parameter and uses a queue object to reverse the elements of the stack.


CODE

#include <bits/stdc++.h>

#include<iostream>

using namespace std;

//function to print the Stack

void Print(stack<int>& Stack)

{

while (!Stack.empty())

{

cout << Stack.front() << " ";

Stack.pop();

}

}

// Function to reverse the Stack

void ReverseStack(stack<int>& Stack)

{

queue<int> Queue;

while (!Stack.empty())

{

Queue.push(Stack.front());

Stack.pop();

}

while (!Queue.empty())

{

Stack.push(Queue.top());

Queue.pop();

}

}

int main()

{

stack<int> Stack;

Stack.push(10);

Stack.push(20);

Stack.push(30);

Stack.push(40);

Stack.push(50);

Stack.push(60);

Stack.push(70);

Stack.push(80);

Stack.push(90);

Stack.push(100);

cout<<"\t\t\t\tShow ReverseStack  "<<endl;

cout<<endl;

ReverseStack(Stack);

Print(Stack);

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.