(Recursively Print a List Backward) Write a member function printListBackward that re- cursively outputs the items in a linked list object in reverse order. Write a test program that creates a sorted list of integers and prints the list in reverse order.
What will be an ideal response?
```
// List2 class template definition.
#ifndef LIST2_H
#define LIST2_H
#include
using namespace std;
#include "ListNode.h"
#include "List.h"
template< typename NODETYPE >
class List2 : public List< NODETYPE >
{
public:
void recursivePrintReverse() const;
private:
void recursivePrintReverseHelper( ListNode< NODETYPE > * ) const;
}; // end class template List2
// print a list backward recursively.
template< typename NODETYPE >
void List2< NODETYPE >::recursivePrintReverse() const
{
cout << "The list printed recursively backward is:\n";
recursivePrintReverseHelper( firstPtr );
cout << '\n';
} // end function recursivePrintReverse
// helper for printing a list backward recursively.
template< typename NODETYPE >
void List2< NODETYPE >::recursivePrintReverseHelper(
ListNode< NODETYPE > *currentPtr ) const
{
if ( currentPtr == 0 )
return;
recursivePrintReverseHelper( currentPtr->getNextPtr() );
cout << currentPtr->getData() << ' ';
} // end function recursivePrintReverseHelper
#endif
```
```
#include "List2.h"
int main()
{
List2< int > intList;
// insert into list
for ( int i = 1; i <= 10; i++ )
intList.insertAtBack( i );
intList.print();
intList.recursivePrintReverse();
return 0; // indicates successful termination
} // end main
```
The list is: 1 2 3 4 5 6 7 8 9 10
The list printed recursively backward is:
10 9 8 7 6 5 4 3 2 1
Destroying nodes ...
1 2 3 4 5 6 7 8 9 10
All nodes destroyed
You might also like to view...
The "has a" relationship is sometimes called a(n) __________ because one object is part of a greater whole.
a. enterprise b. possession c. mutual relationship d. whole-part relationship
Which software program is an example of a web browser?
A. Google Chrome B. YouTube C. Really Simple Syndication D. LinkedIn
The best way to apply Page Setup options to multiple worksheets is to ________.
A. group the worksheets B. use the Format Painter C. copy and Paste the formatting codes from one sheet to another D. use the Apply to Selected Sheets formatting command
In the figure above, the Soups document is ____ pages long.
A. six B. eight C. thirty-four D. twenty-seven