Information

Author(s) Maxime Mawait & Nicolas Rybowski
Deadline No deadline
Submission limit No limitation
Category tags s3, category_linked_list, category_malloc, category_struct, level3

Tags

Sign in

[S3] Simple linked list

We ask you to write two simple functions that are needed to implement a simple linked list.

/**
* Structure node
*
* @next: pointer to the next node in the list, NULL if last node_t
* @value: value stored in the node
*/
typedef struct node {
  struct node *next;
  int value;
} node_t;

/**
* Structure list
*
* @first: first node of the list, NULL if list is empty
* @size: number of nodes in the list
*/
typedef struct list {
  struct node *first;
  int size;
} list_t;

In your functions, you cannot use the function calloc(3)

NB : Do not forget to verify the returned value of malloc and don't forget to manage the error cases as mentioned in the following specifications.


Question 1: Node initialisation
/*
* Create a new node containing the value @value.
*
* @value: value stored in the node
* @next : pointer to next node, initialised to NULL.
* @return: returns the pointer to the newly created node, or NULL if an error occured.
*/
node_t *init_node(int value) {
Question 2: Adding a node
/*
* Add a node at the head of the list @list and increment the number of nodes in the list (member `size`)
*
* @l: linked list
* @value: value to add to the list
*
* @return: 0 if success, 1 otherwise
*/
int add_node(list_t *list, int value) {

//Hint : Don't hesitate to reuse the function you defined above !