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

If someone could help it would be greatly appreciated. Thanks ) [3 points] Downl

ID: 3663398 • Letter: I

Question

If someone could help it would be greatly appreciated. Thanks

) [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 void reverseStack(stackmyStack); 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. 2 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

/**
* Stack.cpp - This file uses the C++ STL stack API and implements a
* helper function called reverse that will reverse a
* given stack.
*
* TODO: Include your name and course number here.
*/

#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

#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)
{

Object item;

stack <Object> tmpStack;

while(!myStack.empty())

{

item=myStack.top();

myStack.pop();

tmpStack.push(item);

}

myStack=tmpStack;

return;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote