Ercument
Ercument

Reputation: 19

C Linked list, losing elements

I'm trying to write code that adds a linked list's element's numbers. But after addition I lose some of the elements. I can't find the errors. Here's my code:

void push(struct node** head_ref, int new_data){
    struct node* new_node = (struct node*)malloc(sizeof(struct node*));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}
void reverse(struct node** head_ref){
    struct node* prev   = NULL;
    struct node* current = *head_ref;
    struct node* next;
    while (current != NULL){
        next  = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head_ref = prev;
}
struct node *shiftter(struct node *a, int index){
    struct node *temp = a;
    while(index > 0){
        append(&temp, 0);
        --index;
    }
    return temp;
}
struct node *add(struct node *fist, struct node *second){
    struct node *gudu = second;
    struct node *c = fist;
    struct node *hudu = NULL;
    reverse(&gudu);
    reverse(&c);
    while(c != NULL){
        push(&hudu, c->data + gudu->data);
        c = c->next;
        gudu = gudu->next;
    }
    while(gudu != NULL){
        push(&hudu, gudu->data);
        gudu = gudu->next;
    }
    return hudu;
}
int main(int argc, const char * argv[]) {
    struct node *a = NULL;
    struct node *b = NULL;
    push(&a , 1);
    push(&a , 2);
    push(&a , 3);
    push(&a , 4);
    push(&b , 5);
    push(&b , 1);
    push(&b , 2);
    push(&b , 4);
    printList(a);
    printf("\n");
    printList(b);
    printf("\n");
    b = shiftter(b,1);
    printList(b);
    printf("\n");
    printList(add(a, b));
    printf("\n");
    printList(a);
    printf("\n");
    printList(b);
    return 0;
}

My output is:

4 3 2 1 

4 2 1 5 

4 2 1 5 0  

4 6 4 7 1 

4

4 

My program ended with exit code: 0

Upvotes: 0

Views: 277

Answers (2)

chqrlie
chqrlie

Reputation: 145297

Even if the algorithm in reverse was correct, the problem in function add is simple: you reverse the lists and iterate through the resulting lists in parallel. But you do not keep the new heads, so all nodes but the last are no longer referred to by anything.

You should keep the new heads and reverse them back to restore the original lists after you are done with the computation.

Better even: keep the lists in low to high digit order.

Upvotes: 2

autistic
autistic

Reputation: 15642

For a start, your function named reverse doesn't do anything remotely close to reverseing a list. It puts the last element (prev) at the head of the list, but that's as close as it gets... The rest of the logic is cloudy at best.Shouldn't it be modifying the last element's ->next member, to point to the second-to-last element? This is where I believe the source of your missing elements is.

P.S. We still don't have the complete program necessary to definitively answer this question. Please ping me once you update the question with a complete/compilable testcase.

Upvotes: 0

Related Questions