Doubly Linked List Maintenance (Optional)
Time limit: 5000ms
Memory limit: 256mb
Description:
In this exercise, you are required to maintain a doubly linked list which contains distinct integers in ascending order, and support the following operations.
1. Insert an integer.
2. Find the position of an integer.
3. Delete an integer.
4. Print all integers in descending order.
The main function has been provided. It first asks the user to input the integers and invokes the Insert function to add the integers to the list. Then it asks for a value that the user wants to find, and invokes the Find function to find the node that contains the value, and prints out the two values before and after the search value. Finally it asks for a value for deletion, invokes the Delete function, and then prints the list in descending order via the Print_DESC function.
You need to complete four functions.
List Insert(List p, int value);
Insert the value into doubly linked list p and return the updated head and tail pointers.
You could assume that the value doesn't exist in p before insertion.
After insertion, the integers in p should still be in ascending order.
Node* Find(List p, int value);
Find the node which contains the value on the doubly linked list p and return its address.
You could assume that the value exists in p.
List Delete(List p, int value);
Delete value from p and return the updated head and tail pointers.
You could assume that the value exists in p before deletion.
After deletion, the integers in p should still be in ascending order.
To delete the value, you may use the function Find to locate it fisrt.
void Print_DESC(List p);
Print all integers in descending order in one line. Every two integers are separated by a space.
Sample Input 1:
4
1 7 4 10
7
4
Sample Output 1:
Input the number of integers inserted into the linked list:
Input these integers:
Input the value to find:
4 7 10
Input the value for deletion:
10 7 1
Sample Input 2:
6
11 5 6 3 7 1
1
1
Sample Output 2:
Input the number of integers inserted into the linked list:
Input these integers:
Input the value to find:
NULL 1 3
Input the value for deletion:
11 7 6 5 3
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct doubly_linked_list_node{
int value;
struct doubly_linked_list_node *prev, *next;
} Node;
typedef struct{
Node *head; // point to the first node of doubly linked list
Node *tail; // point to the last node of doubly linked list
} List;
void InitList(List *p)
{
p->head = NULL;
p->tail = NULL;
}
List Insert(List p, int value)
{
//Insert the value into doubly linked list p and return the updated head and tail pointers.
}
Node* Find(List p, int value)
{
//Find the node which contains the value on the doubly linked list p and return its address.
}
List Delete(List p, int value)
{
//Delete value from p and return the updated head and tail pointers.
Node* ptr = Find(p, value);
}
void Print_DESC(List p)
{
// print all integers in descending order
}
int main()
{
List p;
int n;
int value;
Node* ptr;
InitList(&p);
printf("Input the number of integers inserted into the linked list:\n");
scanf("%d", &n);
printf("Input these integers:\n");
for(int i=0; i<n; i++)
{
scanf("%d", &value);
p = Insert(p, value);
}
printf("Input the value to find:\n"); // print the integers before and after the given value
scanf("%d", &value);
ptr = Find(p, value);
if(ptr->prev!=NULL) printf("%d", ptr->prev->value);
else printf("NULL");
printf(" %d ", ptr->value);
if(ptr->next!=NULL) printf("%d", ptr->next->value);
else printf("NULL");
printf("\n");
printf("Input the value for deletion:\n");
scanf("%d", &value);
p = Delete(p, value);
Print_DESC(p);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit