-1

I created a linked list function which return a pointer to the node. Then I iterated n amount times to accept the number from the user and store it in a linked list, connecting all the nodes. But it is only printing the first and last node. See my code below:

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

typedef struct node {
    int number;
    struct node *next;
} nd;

nd* create(int num);

int main(void) {
    int n;
    printf("How many numbers do you intend on entering ");
    scanf("%d", &n);
    int num;
    nd* bg = NULL;

    for (int i = 0; i < n; i++) {
        printf("Enter a number");
        scanf("%d", &num);
        nd *ls = malloc(sizeof(nd));
        ls = create(num);
    
        if (i == 0) {
            bg = ls;
        }
        else {
            nd *p = malloc(sizeof(nd));
            p = bg;
            p->next = ls;
        }
    }

    for (nd *k = bg; k != NULL; k = k->next) {
        printf("%d\n", k->number);
    }
}

nd* create(int num) {
    nd *list = malloc(sizeof(nd));

    if (list == NULL) { // Check to if we have ran out of memory
        return 0;
    }

    list->number = num;
    list->next = NULL;

    return list;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Aug 21 '22 at 06:07
  • 3
    You are mallocing memory, setting it up & throwing it away to replace it with different malloc'ed memory all over the place. Slow down. Take a deep breath & calm down. Then Slowly read through your code & explain it to your rubber duck. By the time you go through this, you should see how to straighten things out. – Avi Berger Aug 21 '22 at 06:07
  • `nd *p = malloc(sizeof(nd)); p = bg;` Are you coming from Java by any chance? – n. m. could be an AI Aug 21 '22 at 06:17
  • Move your print loop inside the for() loop, and you will see what is happening as the LL grows (such as it does...) This might provide some insights... – Fe2O3 Aug 21 '22 at 06:18
  • @aulven yep that's me misreading the code. – n. m. could be an AI Aug 21 '22 at 06:36

1 Answers1

1

First thing I notice: create allocates memory for a new node. So why allocate memory before that for the same pointer?

        nd *ls = malloc(sizeof(nd));
        ls = create(num);

The results of this first malloc, if it succeeded, are now unable to be freed as we do not have a pointer to it.

Within your loop, you have created a node ls and then another p.

Slow down and think through what you're doing. In your loop:

  • You prompt for a number.
  • Create a node with that data.
  • If your head node bg is NULL you have bg point to that node.
  • Otherwise you link that node to the previous node.
int main(void) {
    int n;
    printf("How many numbers do you intend on entering ");
    scanf("%d",&n);

    int num;
    nd *bg = NULL;
    nd *cur, *prev;

    for (int i = 0; i < n; i++) {
        printf("Enter a number");
        scanf("%d",&num);
        cur = create(num);

        if (!bg) {
            bg = cur;
        }
        else {
            prev->next = cur;
        }

        prev = cur;
    }

    for (nd *k = bg; k != NULL; k = k->next) {
       printf("%d\n",k->number);
    }

    return 0;
}

You'll also want to walk your list and free all of the memory you've allocated.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Thanks. One question, I would like you to please expound on the point, "The results of this first malloc, if it succeeded, are now unable to be freed as we do not have a pointer to it.", please. I am entirely new to linked list but Have vast knowledge of pointers – Matthew Manning Aug 22 '22 at 05:29
  • You have to pass a pointer to `free`. If I `nd *a = malloc(sizeof(nd));` then `a` points to the memory allocated (we're assuming `malloc` succeeded). If I then have `a = some_other_node_ptr;` there is no longer a pointer to the memory I allocated in the first statement. I can no longer `free` that memory. – Chris Aug 22 '22 at 05:58