Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I really need help with the reverse stack part! That is the only part that needs

ID: 3662642 • Letter: I

Question

I really need help with the reverse stack part! That is the only part that needs modifying. The code is below.

[3 points] Download the Stack.cpp file. Modify the to implement details for the helper function called reverse(). The prototype for this function is as follows:

template <class Object> void reverseStack(stack<Object> myStack);

This implementation will also use the C++ Standard Template Library. Specifically the stack API. The function takes a stack called myStack as input, and will reverse the items and store the results back into original stack.

Hint: You only need to modify the areas of the code in the Stack.cpp file that are marked with TODO comments. Everything else in the program will remain that same. Use the stack API’s top(), pop(), and push() functions. Consider using a temporary stack to temporarily hold the results. The printStack() function provides an example on how to use a while-loop to walk through the stack using the pop() and top() operations on a temporary stack. For this problem, the while-loop can walk through the main stack by calling top() and pop() operations on the main stack. Within the loop add each item to the temporary stack by calling the push() operation. After the while-loop, set myStack equal to the temporary stack.

Output: The output for the program after the function is implemented should appear as follows:

The contents of myStack: jumped fox brown quick the

The reverse of myStack: the quick brown fox jumped

** Press any key to continue **

Below you will find the code to modify for the stack.

* Stack.cpp - This file uses the C++ STL stack API and implements a
*            helper function called reverse that will reverse a
*            given stack.
*
*
*/

#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())
   {
      // Print the top item that is on the stack
      cout << " " << tmpStack.top() << endl;

      // Pop the item off of the stack
      tmpStack.pop();
   }

   cout << endl;

   return;
}

template <class Object>
void reverseStack(stack<Object> &myStack)
{
   // TODO: Implement the details for the reverseStack function.

   return;
}

Explanation / Answer

I have written this below function..this function will reverse the given stack of stl set

template <class Object>
void reverseStack(stack<Object> &myStack)
{

   Object item;
   stack<Type> myStack1;

   while (myStack.isEmptyStack() == false)
   {
      item = myStack.top();
      myStack.pop();
      tmpStack.push(item);
   }
   myStack = tmpStack;
   return;
}