Information

Author(s) Mathieu Xhonneux, Nicolas Rybowski
Deadline No deadline
Submission limit No limitation
Category tags s4, category_pointer, category_malloc, category_struct, level4

Tags

Sign in

[S4] Filtering a linked list

You have a linked list. Each element of the list is a struct node.

struct node {
    struct node *next;
    int hash;
    int id;
    char name[20];
    char buffer[100];
    unsigned int timestamp;
    char acl;
    short flow;
    char *parent;
    void *fifo;
};

Question 1: Includes

Please insert here the #include that are required to use the functions that you use to answer this question. These includes are specified in the man pages of the functions that you use.

Question 2: Filtering

We want to filter this list and extract the elements with an even index (the index starts at 0, hence the first, the third, the fifth, ... elements of the linked list have to be taken), without modifying the initial list. In other words, you should create an other list (in parallel of the initial one) that contains the nodes you want to keep and return the head of this new list you created.

Write the body of the function pair_filter.

Hint : You should use memcpy(3) for this question

/* pair_filter
 * Make a copy of the linked list starting at head,
 * only taking the elements with an even index
 *
 * @head : head of a linked list, possibly NULL
 * @return: pointer to the filtered linked list, return NULL if error or if head == NULL
 */
struct node *pair_filter(struct node *head) {