0

I have the following code to create one-way list of user-specified number of elements

typedef struct Node{
    int val;
    struct * Node next;
} Node;

int main(){
    int num;
    Node * prev = NULL;
    Node * first = NULL;
    while(scanf("%d", num) != EOF){
        Node node;
        node.val = num;
        if(prev == NULL){
            prev->next = &node;}
        prev = &node;
        if(first == NULL){
            first = &node;}
    }
}

but this seems to create 1 node and edit it each iteration

as far as I understand new() exists only in c++, is there any way to do this without making array via malloc() and reallocating it every iteration?

6
  • 1
    You try to create a linked list. Why would you use an array and realloc it? That is something very different. Instead you can use malloc to create each new node and link to previous one. Similar as with objects created via new operator. So, why do you want to avoid malloc?
    – Gerhardh
    Commented Feb 12 at 8:38
  • @user98967885655 This if statement if(prev == NULL){ prev->next = &node;} invokes undefined behavior. Commented Feb 12 at 8:41
  • Avoiding malloc just complicates the problem. Just use malloc, it's the most straightforward way Commented Feb 12 at 8:44
  • 1
    scanf("%d", num) enable compiler warnings and pay attention to them.
    – Lundin
    Commented Feb 12 at 9:01
  • 1
    There are different approaches. You have to decide what fits your use case. The most common way is to dynamically allocate every node separately using dynamic allocation using e.g. malloc. If you can define an upper limit for the number of nodes, you could also use a static array of nodes, but in this case you might need a flag to mark a node as free or used. Or if you don't have an upper limit, you could also dynamically realloc an array of nodes. This might improve the performance if you allocate bigger chunks instead of one by one.
    – Bodo
    Commented Feb 12 at 9:53

2 Answers 2

0

Think about the scope. When you use a while loop in your program, a pair of curly braces is used. It means that your newly created node can only survive between the curly breaces. Every time the loop body is carried out, the memory will be recycled. Note that when you get in the loop body, the system allocate the memory from stack, which will be recycled. But malloc() will allocate from the heap, which need to be managed by your self. So malloc() can continuously gives you a new node. Hope it's helpful! :)

0

Your code is invalid and results in undefined behavior.

For example if the pointer prev is a null pointer then you may not dereference it as you are doing in this if statement

    if(prev == NULL){
        prev->next = &node;}

Moreover this declaration

Node node;

is alive only in each iteration of the while loop. So pointers that point to the object node will not be valid after the while loop.

And this call of scanf

scanf("%d", num)

is invalid. Instead you shall write

scanf("%d", &num)

How to dynamically create multiple instances of struct in loop in C?

It seems you want something like the following.

Node *head = NULL;

int num;
for ( Node **current = &head; 
      scanf("%d", &num) == 1 && ( *current = malloc( sizeof( Node ) ) ) != NULL;
      current = &( *current )->next )
{
    ( *current )->val = num;
    ( *current )->next = NULL;
}

To free all the allocated nodes when the list will not be required any more you can write the following while loop

while ( head != NULL )
{
    Node *tmp = head;
    head = tmp->next;
    free( tmp );
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.