Linked List Management 2023 (Required)

Time limit: 500ms
Memory limit: 256mb

Description:
In this exercise, you are required to maintain a linked list of sorted distinct integers in descending order and support the following operations.

1. Insert an integer to the linked list if it does not yet occur in the list. If already exist, do nothing.
2. Delete an integer from the head of the linked list
3. Delete an integer from the end of the linked list
4. Delete an integer from the middle elements of the linked list
5. Delete all matching integers in a range (exclusive) from the linked list

The program performs Delete and Insert operations on the integers input.

The code template has been provided. Your task is to complete the following functions.

List Insert(List p, int value);
Firstly, check whether the value already exist in the list p, if not exist yet, do the insertion, do nothing otherwise.

Example:
Insert 1 to list 9 -> 5 -> 4 -> 2 -> 1
List then becomes: 9 -> 5 -> 4 -> 2 -> 1 (as 1 already exist)

Insert 6 to list 9 -> 5 -> 4 -> 2 -> 1
List then becomes: 9 -> 6 -> 5 -> 4 -> 2 -> 1

List Delete_a(List p);
Delete the node from the head of the linked list p. Assume that the list is not empty before deletion.

Example:
Delete from the head of list 9 -> 6 -> 5 -> 4 -> 2 -> 1
List then becomes: 6 -> 5 -> 4 -> 2 -> 1

List Delete_b(List p);
Delete the node from the end of the linked list p. Assume that the list is not empty before deletion.

Example:
Delete from the end of list 9 -> 6 -> 5 -> 4 -> 2 -> 1
List then becomes: 9 -> 6 -> 5 -> 4 -> 2

List Delete_c(List p);
Delete the node from the middle of the linked list p. Assume that the list is not empty before deletion. If there are even number of integers, delete the FIRST middle element. (Tips: You can make use of the functions Num(List p) and MiddleIndex(List p) which returns the number of distinct values and the first middle index of the list, respectively)

Example:
Delete the FIRST middle integer from list 9 -> 6 -> 5 -> 4 -> 2 -> 1 (Here, there are even number of integers, the FIRST middle integer is 5 and the SECOND middle integer is 4)
List then becomes: 9 -> 6 -> 4 -> 2 -> 1

Delete the middle integer from list 9 -> 6 -> 4 -> 2 -> 1
List then becomes: 9 -> 6 -> 2 -> 1

List Delete_d(List p, int value_min, int value_max);
Delete all matching integers with values in a range (exclusive) from value_min to value_max from the linked list p. 

Example:
Given value_min=2, value_max=6, and list 9 -> 6 -> 5 -> 4 -> 2 -> 1. Delete all matching integers in the list with values greater than 2 (> 2) and less than 6 (< 6)
List then becomes: 9 -> 6 -> 2 -> 1

Given value_min=6, value_max=9, the integers with values greater than 6 (> 6) and less than 9 (< 9) will be deleted from list 9 -> 6 -> 5 -> 4 -> 2 -> 1
List then becomes: 9 -> 6 -> 5 -> 4 -> 2 -> 1

Sample input
10
2 4 6 5 3 8 6 7 11 9
3 7

Sample output:
9 8 7 3

-------------------------Copy the following code, complete it and submit-------------------------

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct linked_list_node{
    int value;
    struct linked_list_node* next;
} Node;

typedef Node* List;

int Exist(List p, int value)
{
    // judge whether linked list p contains the value
    // you should visit each node of the linked list one by one, and check whether it is equal to the value
    // if you find it then return 1, otherwise return 0
    while(p!=NULL)
    {
        if(p->value==value) return 1;
        p = p->next;
    }
    return 0;
}

void Print(List p)
{
    // print all integers from the head of linked list in one line(separated by a space), in other words, you should firstly print the integer p points to
    // to be more specific, print p->value and then move p to the next node p->next recursively, until p points to NULL
    while(p!=NULL)
    {
        printf("%d ", p->value);
        p = p->next;
    }
    printf("\n");
}

int Num(List p)
{
    // get the number of elements in the list
    int num = 0;

    while(p!=NULL)
    {
        num = num + 1;
        p = p->next;
    }

    return num;
}

int MiddleIndex(List p)
{
    
}

List Delete_a(List p)
{

}

List Delete_b(List p)
{
    
}

List Delete_c(List p)
{
    
}

List Delete_d(List p, int value_min, int value_max)
{
    
}

List Insert(List p, int value)
{
    
}

int main()
{
    List p = NULL;
    int n, m;
    int value, value_min, value_max;

    // List initialization
    // How many integers in list initially
    scanf("%d", &n);
    // What are the integers
    for(int i=0; i<n; i++)
    {
        int value;
        scanf("%d", &value);
        p = Insert(p, value);
    }
    

    // Delete a value from head
	p = Delete_a(p);
    
    // Delete a value from end
	p = Delete_b(p);
    
    // Delete a value from first middle
	p = Delete_c(p);
    
    // Delete the values in a range
    scanf("%d%d", &value_min, &value_max);
    p = Delete_d(p, value_min, value_max);

    // print current list
    Print(p);

    return 0;
}

-------------------------------------------End of Code-------------------------------------------
Submit