Question
I need help writing a c++ RECURSIVE print function for a singly linked list that will print the list in reverse. The linked list uses
I need help writing a c++ RECURSIVE print function for a singly linked list that will print the list in reverse. The linked list uses a template class and a struct Node:
template
struct Node {
Type data;
Node *next;
};
I have all functions, like constructor, destructor, push, pop, etc working.
This function needs to be written using the friend std::ostream& operator<< <>(std::ostream&, const Stack& stack);
here are more instructions for this function:
Outputs the items in the stack formated as item1->item2->...->itemn. For example, if the stack is 1,2,3,4 with the 4 on top of the stack, this method outputs 1->2->3->4. Note that this method prints in order from the bottom of the stack to the top of the stack. It is recommended that you use recursion to accomplish this. See the notes below. When implementing the stack it is recommended that you use the head of the list as the top of the stack. This makes the pop method more efficient. If you do not implement the top of the stack as the head of the list, you will not receive full credit for this assignment. To print the stack from bottom to top it is recommended that you use recursion to do so. This is not strictly required but is strongly recommended. Printing a stack recursively from bottom to top works like this. Print the rest of the stack recursively Print the current node of the stack.
I can't seem to figure out how to use the std::ostream& operator<<(std::ostream& out, const Stack& list) to do this and have been trying to implement a helper function to assist - maybe something like string recursive_str(Node *curr) that would use recurrsion and return the data back into the overloaded << function. So far, I can't seem to get anything to work quite right. Please help!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started