Information

Author(s) Jean-Pierre Sagdic Anthony Doeraene
Deadline No deadline
Submission limit No limitation

Tags

Sign in

To the matrix

You have to write a function called linked_list_to_matrix which returns a matrix of size n*m (n rows, m columns) containing the values of the linked list lst. For example, with the linked list containing the following values : first -> 1-2-3-4-5-6-7-8-9

pour n=3, m=3 renvoie : [[1 2 3]
                         [4 5 6]
                         [7 8 9]]

pour n=2, m=3 renvoie : [[1 2 3]
                         [4 5 6]]

pour n=3, m=4 renvoie : [[1  2  3  4]
                         [5  6  7  8]
                         [9 -1 -1 -1]]

Note that if there isn't enough values in the linked list to fill the matrix, the remaining empty cells should contain -1.

Here's the definition of the linked list :

typedef struct node{
    struct node* next;
    int value;
} node_t;

typedef struct list{
    struct node* first;
    int size;
} list_t;

Linked List to Matrix
/*
 *@pre lst!=NULL, n>0, m>0
 *@post return a matrix of size n*m containing the values of the linked list
 *      return NULL if malloc fails
 */
int** linked_list_to_matrix(list_t *lst, int n, int m);