I need help reversing a stack using the pop(), top(), and push() functions, if someone could help point me in the right direction. Thanks!
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
template <class Object>
void print(stack<Object> myStack);
template <class Object>
void reverseStack(stack<Object> &myStack);
int main()
{
stack<string> myStack;
myStack.push("the");
myStack.push("quick");
myStack.push("brown");
myStack.push("fox");
myStack.push("jumped");
cout << "The contents of myStack:" << endl;
print(myStack);
cout << "The reverse of myStack:" << endl;
reverseStack(myStack);
print(myStack);
cout << "** Press any key to continue **";
getchar();
return 0;
}
template <class Object>
void print(stack<Object> myStack)
{
stack<Object> tmpStack;
tmpStack = myStack;
while (!tmpStack.empty())
{
cout << " " << tmpStack.top() << endl;
tmpStack.pop();
}
cout << endl;
return;
}
template <class Object>
void reverseStack(stack<Object> &myStack)
{
return;
}
think about how a stack works.
All you need to do is copy one stack off the top, to another stack.
Then it is reversed.