(Recursively Search a List) Write a member function searchList that recursively searches a linked list object for a specified value. The function should return a pointer to the value if it is found; otherwise, null should be returned. Use your function in a test program that creates a list of integers. The program should prompt the user for a value to locate in the list.

What will be an ideal response?


```
// ListNode class template definition.
#ifndef LISTNODE_H
#define LISTNODE_H

template< typename T > class List; // forward declaration

template< typename NODETYPE >
class ListNode
{
friend class List< NODETYPE >; // make List a friend
public:
ListNode( const NODETYPE & ); // constructor
NODETYPE getData() const; // return the data in the node

// set the next pointer
void setNextPtr( ListNode *nPtr )
{
nextPtr = nPtr;
} // end function setNextPtr

// get the next pointer
ListNode *getNextPtr() const
{
return nextPtr;
} // end function getNextPtr

// get the address
NODETYPE *getAddress()
{
return &data;
} // end function getAddress
private:
NODETYPE data; // data
ListNode *nextPtr; // next node in the list
}; // end class ListNode

// constructor
template< typename NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
{
data = info;
nextPtr = 0;
} // end ListNode constructor

// return a copy of the data in the node
template< typename NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const
{
return data;
} // end getData

#endif
```
```
// List2 class template definition
#ifndef LIST2_H
#define LIST2_H

#include
#include
#include "ListNode.h"
#include "List.h"
using namespace std;

template< typename NODETYPE >
class List2 : public List< NODETYPE >
{
public:
NODETYPE *searchList( NODETYPE & ) const;
private:
// utility function
NODETYPE *recursiveSearchHelper(
ListNode< NODETYPE > *, NODETYPE & ) const;
}; // end class List2

// Search a List recursively.
template< typename NODETYPE >
NODETYPE *List2< NODETYPE >::searchList( NODETYPE &val ) const
{
return recursiveSearchHelper( firstPtr, val );
} // end function recursiveSearch

// Helper for searching a list recursively.
template< typename NODETYPE >
NODETYPE *List2< NODETYPE >::recursiveSearchHelper(
ListNode< NODETYPE > *currentPtr, NODETYPE &value ) const
{
if ( currentPtr == 0 )
return 0;

// if data is found
if ( currentPtr->getData() == value )
return currentPtr->getAddress();

// continue recursive call
return recursiveSearchHelper( currentPtr->getNextPtr(), value );
} // end function recursiveSearchHelper

#endif
```
```
#include
using namespace std;

#include "List2.h"

int main()
{
List2< int > intList;

// create list
for ( int i = 2; i <= 20; i += 2 )
intList.insertAtBack( i );

intList.print(); // print list

int value;
int *ptr; // int pointer

cout << "Enter a value to search for: ";
cin >> value;
ptr = intList.searchList( value );

if ( ptr != 0 )
cout << *ptr << " was found\n";
else
cout << "Element not found\n";

return 0; // indicates successful termination
} // end main
```
The list is: 2 4 6 8 10 12 14 16 18 20
Enter a value to search for: 18
18 was found
Destroying nodes ...
2 4 6 8 10 12 14 16 18 20
All nodes destroyed

Computer Science & Information Technology

You might also like to view...

Answer the following questions true (T) or false (F)

1. Every function prototype must include at least one formal parameter. 2. A function call is a valid statement. 3. A user-defined function can call library functions or user-defined functions.

Computer Science & Information Technology

Which of the following two examples demonstrate strong alignment, the first or the second? Justify your answer in terms of virtual lines.

Computer Science & Information Technology

Suppose that we want to store digitized audio information in a binary file. An audio signal typically does not change much from one sample to the next. In this case, less memory is used if we record the change in the data values instead of the actual data values. We will use this idea in the following program. Write a program StoreSignal that will read positive integers, each of which must be within 127 of the previous integer, from the keyboard (or from a text file, if you prefer). Write the first integer to a binary file. For each subsequent integer, compute the difference between it and the integer before it, cast the difference to a byte, and write the result to the binary file. When a negative integer is encountered, stop writing the file.

What will be an ideal response?

Computer Science & Information Technology

PreparedStatement method ________ returns a ResultSet.

a. executeUpdate b. executeQuery c. execute d. None of the above.

Computer Science & Information Technology