-1

When I execute the code it doesn't print the list, so adding the IF inside of the Lread function I discovered that *list is still NULL when I read it! Can you tell me why the Lcreate function fail to create the list? Thank you very much!!

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

typedef struct _elem {
    int key;
    struct _elem *next;
} elem;

elem* Lcreate(elem *list) {
    int listsize;
    scanf("%d",&listsize);
    elem *p = NULL;
    p = list;
    elem *nuovo = NULL;
    int counter=0;
    while(counter<listsize){
        nuovo = (elem *) malloc(sizeof(elem));
        scanf("%d",&(nuovo->key));
        nuovo->next=NULL;
        if(p==NULL){
            p=nuovo;}
        else{
            while(p->next!=NULL){
                p=p->next;}
            p->next=nuovo;}
        counter++;}
}

elem* Lread(elem *list){
    if (list==NULL){
        printf("error\n");
    }
    elem *p = list;
    while(p!=NULL){        
        printf("%d\n",p->key);
        p=p->next;
    }
}


main(){
    struct _elem *list=NULL;
    Lcreate(list);
    Lread(list);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • same problem, but hard to see the similarity: https://stackoverflow.com/questions/10907650/add-to-array-call-results-in-null-list – Yunnosch Aug 20 '19 at 16:59
  • Better, if you can handle an answer without coding solution: https://stackoverflow.com/questions/20108412/c-linked-list-why-is-my-list-head-variable-remaining-null-new-to-c – Yunnosch Aug 20 '19 at 17:02
  • 2
    @Alessandro The function Lcreate returns nothing though its return type is not void. – Vlad from Moscow Aug 20 '19 at 17:03
  • And it can sometimes be very useful to return something! – joop Aug 20 '19 at 17:07
  • These two give code solutions for the two most obvious solutions. https://stackoverflow.com/questions/28498069/linked-list-for-stack-head-next-keeps-becoming-null and https://stackoverflow.com/questions/36338968/inserting-element-at-start-of-linked-list-works-in-function-but-on-return-from The second one better matches your prototype. – Yunnosch Aug 20 '19 at 17:10
  • Possible duplicate of [Inserting element at start of linked list works in function, but on return from function the start point is NULL](https://stackoverflow.com/questions/36338968/inserting-element-at-start-of-linked-list-works-in-function-but-on-return-from) – Yunnosch Aug 20 '19 at 17:10

1 Answers1

1

For starters the function Lcreate returns nothing though its return type is elem *.

elem* Lcreate(elem *list);

Within the function the original pointer to the head of the list is not changed.

It is better when the function returns the number of added elements to the list.

The function can be defined the following way

size_t Lcreate( elem **list ) 
{
    size_t listsize;

    scanf( "%zu", &listsize );

    for ( size_t i = 0; i < listsize; i++ )
    {
        *list = ( elem * )malloc( sizeof( elem ) );

        scanf( "%d", &( *list )->key );
        ( *list )->next = NULL;

        list = &( *list )->next;
    }

    return listsize;
}

and called like

elem *list = NULL;
Lcreate( &list );

or

elem *list = NULL;
size_t n = Lcreate( &list );

Or you can add a check into the function that a next node was successfully allocated. For example

size_t Lcreate( elem **list ) 
{
    size_t listsize;

    scanf( "%zu", &listsize );

    size_t i = 0;

    for ( ; i < listsize && ( *list = malloc( sizeof( elem ) ) ) != NULL; i++ )
    {
        scanf( "%d", &( *list )->key );
        ( *list )->next = NULL;

        list = &( *list )->next;
    }

    return i;
}

Pay attention to that the header <malloc.h> is not a standard header. It should be removed. The allocation functions are declared in the header <stdlib.h>.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335