Thông tin

Tác giả Maxime Mawait & 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, Level 4, Pointer, Linked list, Malloc, Struct

Tags

Đăng nhập

[S4] Ordered linked list

Given the provided implementation of a linked list, you need to implement the insert function based on a specific order relation. The goal is to implement an ordered list similar to a LinkedList with a Comparator in Java.


Câu hỏi 1: Order relation

For this subproblem, you need to implement the comparison function which defines the order relationship of the list.

Write the body of the function compare.

/*
* @return: 0 if equals, negative number if @b is greater,
* and positive number otherwise
*/
int compare(char a, char b) {
Câu hỏi 2: Insert

Assuming following linked list structure :

typedef struct node{
    char val;
    struct node *next;
} node_t;

Write the body of the function insert

/*
* @value: value to be inserted. If the value is already in the list, no element is added and the list is not modified
* @fun: comparison function which defines the order relationship of the list
* @head: first node of the list, head != NULL
* @return: 0 if success, -1 otherwise
*/
int insert(node_t **head, char val, int (*cmp)(char,char)) {