I'm writing this post after hours of madness and thinking. Probably this is the most stupid exercise you are gonna read today but for me, after hours of exercise, is not like that.

Going back to the question. My professor requested an allocation of a dynamic array inside a linked list. And this point is nothing hard. I wrote the structure and define 2 types. The next step is to write 2 functions:

  1. The first one, called init, creates a new element of the list, allocates the array using an n integer, and returns it to the main;
  2. A print function to show what the arrays have at their inside.

The code looks like that.

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

struct elements{
    int  *array;
    int size;
    struct elements* next; 
};
typedef struct elements elementOfList; 
typedef elementOfList* ListOfElements;

ListOfElements init(int n){
    ListOfElements new;
    new->array = malloc(sizeof(int)*n);
    for(int i = 0; i < n; i++)  new->array[i] = 0;
    new->size = n;
    new->next = NULL;
    return new;
}

void print_list(ListOfElements list){
    if(list == NULL) return;

    printf("%d",list->size);
    print_list(list->next);
}

int main(){

    ListOfElements list = init(4);
    print_list(list);
    // -> n = 4 | 0, 0, 0, 0,

    list->next = init(12);
    print_list(list);
    // -> n = 4 | 0, 0, 0, 0,
    // -> n = 12 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, %

    return 0;
}

As you can see, returning a "listOfElements" to the main makes me do a disaster. At this point, I think I messed up a lot of things. The algorithm went in a loop at the second print. No problem with the printing of the first array but with the second one....shit... (I know I actually don't print the array. I print the size of the array only to make it more readable right now).

I think that my mistake is linked to the "init" function. Something went wrong and I can't understand where. I hope somebody can help or even suggest to me some fixes to the program.

While I wait that somebody to read this post I will try to put on paper what the frick my program is doing.

Thank you for your attention and have a nice day.


Solution 1:

The function init shall allocate an object of the type elementOfList

ListOfElements init(int n)
{
    ListOfElements new = malloc( sizeof( *new ) );
    
    if ( new != NULL )
    {
        new->array = malloc( n * sizeof( int ) );

        if ( new->array == NULL ) n = 0;

        new->size = n;
        for ( int i = 0; i < n; i++ )  new->array[i] = 0;
        // or you can use memset instead of the for loop

        new->next = NULL;   
    }

    return new;
}

and the function print_list should output elements of the inner array. For example

void print_list(ListOfElements list)
{
    if ( list != NULL )
    {
        printf("-> %d | ",list->size);
      
        for ( int i = 0; i < list->size; i++ )
        {
            if ( i != 0 ) printf( ", " ); 
            printf( "%d", list->array[i] );
        }

        putchar( '\n' );

        print_list(list->next);
    }
}