Question: I need help writing a c++ RECURSIVE print function from the ostream& operator < < function: template std::ostream& operator < item2->...->itemn. For example, if the
I need help writing a c++ RECURSIVE print function from the ostream& operator<< function:
template
std::ostream& operator<<(std::ostream& out, const Stack
//CODE GOES HERE
}
This is for a singly linked list that must print in reverse through recursion.
The linked list uses a template class and a struct Node:
template
struct Node {
Type data;
Node *next;
};
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. Use recursion to accomplish this. 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.
The test files use the << operator to call get output, so my std::ostream& operator<< is where the code needs to come from. Additional private functions can be created and called from this function if needed.
I have already implemented all other functions Constructor, Destructor, Copy, Push, Pop, etc for this Stack Class.
I also understand the coding necessary, which is:
/* Function to reverse the linked list */
void printReverse(Node* head)
{
// Base case
if (head == NULL)
return;
// print the list after head node
printReverse(head->next);
// After everything else is printed, print head
cout << head->data << " ";
}
I just need to know how to access it within the ostream& operator<< function
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
