[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.


Question 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) {
Question 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)) {

Information

Author(s) Maxime Mawait & Nicolas Rybowski
Deadline No deadline
Submission limit No limitation
Category Tags Data structures, Pointer, S4

Tags

Sign in