The parameter to the following two recursive routines is a p

The parameter to the following two recursive routines is a pointer to a singly linked list of numbers, whose elements are unique (no duplicates) and unsorted. Each node in the list contains two members, info (a number) and next (a pointer to the next node). Write a recursive value-returning function, MinLoc, that receives a pointer to a list of unsorted numbers and returns a pointer to the node that contains the minimum value in the list. Write a recursive void function. Sort, that receives a pointer to an unsorted list of numbers and reorders the values in the list from smallest to largest. This function may call the recursive MinLoc function that you wrote in part (a).

Solution

Part A

Node* MinLoc (Node* Current) {
   
   Node* NN = Current->next;

if (Current->next == NULL) {
//The value at this node is obviously smaller than a non-existent value
return Current;
}
   else
   {
//Recur to find the smallest value from the rest of the linked list
NN = MinLoc(Current->next);
}

//Return the smallest value between this node and the end of the list
if (Current->value < NN->value)
   {
return Current;
}
   else
   {
return NN;
}
}

Part B

void Sort(Node *Current)
{
   int temp_val; //Temporary variable for swapping data
   Node *Repl; //Temporary node to receive the return value of the function MinLoc
   if(Current->next==NULL)
   {
       exit(0);
   }
   else
   {
       Repl = MinLoc(Current);
       temp_val = Repl->value;
       Repl->value = Current->value;
       Current->value = temp_val;
       Sort(Current->next);
   }
}

 The parameter to the following two recursive routines is a pointer to a singly linked list of numbers, whose elements are unique (no duplicates) and unsorted.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site