Share
Explore BrainMass

Write the function definition as a recursive search using C++

A sequential search member function of SortedType has the following prototype:
void SortedType: : Search(int value, bool& found) ;
a. Write the function definition as a recursive search, assuming a linked list implementation.
b. Write the function definition as a recursive search, assuming an array-based implementation.

Solution Preview

Part a

Recursive search

#include<iostream>
using namespace std;

int recursiveSequential( int a[], int size, int value ) {

if( size == 0 )
return -1;
if( value == a[ size - 1 ] )
return size - 1;
return recursiveSequential( a, size - 1, value );
}

int main () {

int const length = 10;
int searchV;
int list[10] = { 2, 3, 4, 5, 20, 40, 80, 45, 99, 0};

cout << "Please enter the value to be searched" <<endl;
cin>> search;

cout << recursiveSequential(list, length, searchV) << endl;

return 0;
}

using link list function

template<typename T>
Iterator<T> LinkedList<T>::find(T value)
{

Node<T>* pos = NULL;
Iterator<T> res=pos.position;

Node<T>* current = first;
Iterator<T> iter=current.position;

while (current != NULL) ...

Solution Summary

The solution gives a complete C++ code to write the function definition as a recursive search, assuming both a linked list implementation and an array-based implementation.

$2.19