Thông tin

Tác giả Mathieu Xhonneux, Nicolas Rybowski
Hạn chót Không có hạn chót
Giới hạn nộp bài Không có giới hạn
Các tag chuyên mục s4, category_pointer, category_malloc, category_struct, level4

Tags

Đăng nhập

[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;
};

Câu hỏi 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.

Câu hỏi 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) {