this question from C Plus Data Structures By Nell Dale book

this question from (C++ Plus Data Structures By Nell Dale book)

chapter7 ,page490,question21.

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).
(a) [4] 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.
(b) [4] 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). (Hint: it is easier
to swap the values in the info part of the nodes than to reorder the nodes in the list.)

Solution

int lowInfo(ListNode *node){

if(node!=NULL){

int k=lowInfo(node->next);

return (k<node->value)?k:node->value;

}

}

void inSort(struct node **head_reference)

{

    struct node *sorted = NULL;

    struct node *current = *head_reference;

    while (current != NULL)

    {

        struct node *next = current->next;

        sortedInsert(&sorted, current);

        current = next;

    }

    *head_reference = sorted;

}

this question from (C++ Plus Data Structures By Nell Dale book) chapter7 ,page490,question21. The parameter to the following two recursive routines is a pointer

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site