Home > Blockchain >  Any idea why I’m losing links?
Any idea why I’m losing links?

Time:09-22

I have started to learn about linked lists, and I have written this code.

It should be a recursive call to create a new link in a linked list in c.

But, if you’ll check the output, you’ll see it’s passing over the middle links.

I don’t know why I’m losing the middle links.

Btw, I do have a destroy function in my code, I just didn’t write it here.

I do have a different version of a working code, I don’t ask for solutions, I’m only asking why this recursive idea doesn’t work.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct node {
    int data;
    struct node *next;
}node;

node *create(node **head, int data)
{
    if(!*head) {
        *head = malloc(sizeof(node));
        assert(*head);
        (*head)->data = data;
        (*head)->next = NULL;
        return *head;
    }
    node *new = NULL;
    new = create(&new,data);
    (*head)->next = new;
    return *head;
}
void display(node *head)
{
    assert(head);
    node *current = head;
    do
    {
        printf("%d\t",current->data);
        current = current->next;
    }while(current);
}
int main()
{
    int count = 0, data = 0;
    node *head = NULL;

    printf("Enter list count:\n");
    
    while(count <= 0){
        scanf("%d",&count);
        if(count <= 0) printf("\nEnter a valid number:\n");
    }
    while(count){
        scanf("%d",&data);
        head = create(&head,data);
        count--;
    }
    printf("\nHere are the elements:\n");
    display(head);

    return 0;
}

CodePudding user response:

As implemented create() either adds a new node to the tail or iterates to the next linked node. Logic changed to affect that. It's confusing that the first argument is called head to changed it to n. Changed main() to retain the head and made the program non-interactive for ease of testing. Recatored display to use a for() loop:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
} node;

node *create(node **n, int data) {
    if(!*n) {
        *n = malloc(sizeof(**n));
        assert(*n);
        (*n)->data = data;
        (*n)->next = NULL;
        return *n;
    }
    node *n2 = (*n)->next;
    (*n)->next = create(&n2, data);
    return n2;
}

void display(node *head) {
    assert(head);
    for(node *c = head; c; c = c->next) {
        printf("%d\t", c->data);
    }
}

int main() {
    node *head = NULL;
    node *tail = NULL;
    for(int i = 0; i < 10; i  ) {
        tail = create(&tail, i);
        if(!head) head = tail;
    }
    display(head);
    return 0;
}

and it displays:

0       1       2       3       4       5       6       7       8       9       

If you compile your code with NDEBUG (some folks do that for production) then your code no longer has any error handling.

CodePudding user response:

Thank you all for your answers. I see the problem now, after “explaining to the duck” a thousand times. In function create(), under the if() block, I assigned (*head)->next = new; without first making it point to the last link, so it’s just over write the next link in every call to the function. The solution is:

  1. Add a “current” pointer points to the head(to not lose it’s value)
  2. Iterate through the list until we find the last link,
  3. assign current->next the value of new. Here is the fixed section:
    node *new = NULL;
    new = create(&new,data);
    node *current = *head;
    while(current->next) current = current->next;
    current->next = new;
    return *head;
  • Related