0

I'm working on a linked list where I have to find the average of it's elements using a recursive function. However, I am having problems in two of my functions:

1- If I delete the very first node and then try to check the contents, the list spews garbage data, and if I insert something in the front of the list again, it just endlessly repeats showing the first node contents.

2- I can't seem to figure out how to make the recursive average function work.

Here are the snippets of code for both:

void deletefront(head h) {
    head temp;                                  // create a temporary node.
    temp = (head)malloc(sizeof(struct node));  // allocate space for node.
    temp = h;                   // transfer the address of 'head' to 'temp'
    h = temp->next;      // transfer the address of 'temp->next' to 'head'
    free(temp);
}


double average2(head h, int sum, int quantity, double avg) {
head temp3;
temp3 = (head)malloc(sizeof(struct node));
temp3 = h;
if (temp3 == NULL) {
    cout << "List is Empty" << endl;
    return 0;
}
else {
    sum = sum + temp3->data;
    quantity = quantity + 1;
    temp3 = temp3->next;
    if (temp3 == NULL) {
        return avg = sum / quantity;
    }
    else return avg = average2(h, sum, quantity, avg);
}
}

And here's the header:

typedef struct node
{
  int data;               // will store information
  node *next;             // the reference to the next node
}*head;

Note that I have to make sure that the functions can work with any list.

FormigaNinja
  • 1,571
  • 1
  • 24
  • 36
Anstane
  • 177
  • 9
  • Stupid requirements. A linked list fits perfectly sequential processing with `for`/`while` iteration; forcing a recursion makes the solution harder, more complicated, less comprehensible. Additionally, you can't calculate the average of the first item and the rest if you know the average of the rest but not the number of 'the rest' items—so you need to artificially return two values (an average and the count) from the recursive function to complete the task. – CiaPan May 25 '15 at 09:46
  • Side: Unless there is a damn good reason to do otherwise, [don't hide pointer types in `typedef`s](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). It makes the code *less* readable and *more* prone to errors. And I'll assume there is a `typedef struct node node;` *above* that `struct node` definition, otherwise that won't even compile. Finally, `h = temp->next;` isn't doing what you want it to do. It simply changes the *value* stored in the local variable `h`. The *caller's* passed in pointer var remains untouched. – WhozCraig May 25 '15 at 09:49

1 Answers1

1

From deletefront() you should return new head i.e head->next.

head deletefront(head h) {
    head temp;                                      // create a temporary node

    temp = h;                   // transfer the address of 'head' to 'temp'
    h = temp->next;      // transfer the address of 'temp->next' to 'head'
    return h;
}

And you should set returned value to head of your list. Your function just deletes the node in the function, value of h is not changed out of this function.

Also, you don't need to malloc() for temp.


For 2nd problem, update recursive call as

else return avg = average2(temp3, sum, quantity, avg);
//-------------------------^ don't use h
Rohan
  • 52,392
  • 12
  • 90
  • 87