Linked list management
Time limit: 500ms
Memory limit: 256mb
Description:
In this exercise, you are required to maintain a linked list of sorted distinct integers in ascending order and support the following operations.
1. Delete a value from the head of the linked list
2. Delete a value from the end of the linked list
3. Insert a value to the linked list if it does not yet occur in the list. If already exist, do nothing. (Tips: before insertion, check whether the value to be inserted already exist)
Then the program performs Delete and Insert operations. The user will be prompted to input the integers for deletion and insertion.
The code template has been provided .Your task is to complete the following functions.
void Delete_a(List p)
Delete the node from the head of the linked list p. You could assume that the list is not empty.
Example:
Delete from 1 -> 2 -> 3 -> 4 ->5
List will be something like: 2 -> 3 -> 4 ->5
void Delete_b(List p)
delete the node from the end of the linked list p. You could assume that the list is not empty.
Example:
Delete from 1 -> 2 -> 3 -> 4 ->5
List will be something like: 1 -> 2 -> 3 -> 4
void 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 1 -> 2 -> 3 -> 4 ->5
List will be something like: 1 -> 2 -> 3 -> 4 ->5 (as 1 already exist)
insert 6 to 1 -> 2 -> 3 -> 4 ->5
List will be something like: 1 -> 2 -> 3 -> 4 ->5 -> 6 (as 6 not yet exist)
Sample input
10
1 3 5 2 7 11 2 4 8 9
Sample output:
2 3 4 5 7 8 9
-------------------------Copy the following code, complete it and submit-------------------------
#include <stdio.h>
#include <stdlib.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");
}
List Delete_a(List p){
}
List Delete_b(List p){
}
List Insert(List p, int value)
{
}
int main()
{
List p = NULL;
int n;
int value;
// printf("List initialization: \n");
// printf("How many integers in list initially: ");
scanf("%d", &n);
// printf("What are the integers: ");
for(int i=0; i<n; i++)
{
int value;
scanf("%d", &value);
p = Insert(p, value);
}
// printf("Delete a value from head\n");
p = Delete_a(p);
// printf("Delete a value from end\n");
p = Delete_b(p);
// printf("Current array: \n");
Print(p);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit