use c language! Time limit: 2000ms Memory limit: 256mb Description: In this exercise, we use the doubly
Fantastic news! We've Found the answer you've been seeking!
Question:
use c language! Time limit: 2000ms Memory limit: 256mb Description: In this exercise, we use the doubly linked list to maintain a sequence of distinct integers in ascending order. To keep the integers in order after insertion, we can not just insert the new value to the head of the linked list. Instead, we should insert it to an appropriate position. We provide the insert function for you which is called in the main function to create a linked list when users input the numbers in the list. After user input, we have two linked lists which represent two sets of sorted integers denoted by A and B. You are required to implement the following functions. 1. Create a new sorted list as the intersection of A and B. 2. Create a new sorted list as the union of A and B. 3. Print all integers of the linked list in order. List Intersection(List headA, List headB); Generate a linked list head to contain the intersection of A and B in ascending order. You can only visit the input linked list headA and headB but not change them. Finally, return head. List Union(List headA, List headB); Generate a linked list head to contain the union of A and B in ascending order. You can only visit the input linked list headA and headB but not change them. Finally, return head. void Print(List p); Print all integers of linked list p from head to tail in one line. Every two integers are separated by a space. Print a ' ' in the end. If the list contains no integers, output a blank line. Sample Input 1: 4 4 10 1 7 5 7 3 4 11 2 Sample Output 1: Input the number of integers in A: Input these integers: Input the number of integers in B: Input these integers: 1 4 7 10 2 3 4 7 11 The intersection list contains: 4 7 The union list contains: 1 2 3 4 7 10 11 Sample Input 2: 2 2 3 3 6 5 1 Sample Output 2: Input the number of integers in A: Input these integers: Input the number of integers in B: Input these integers: 2 3 1 5 6 The intersection list contains: The union list contains: 1 2 3 5 6 Hint: Be careful of the efficiency issue. You'd better not use the insert function provided to generate the intersection/union linked list. -------------------------Copy the following code, complete it and submit------------------------- #include #include typedef struct node Node; struct node { int v; Node *next; Node *prev; }; typedef struct linked_list List; struct linked_list { Node *head; Node *tail; }; List* Create_List() { List* L; L = (List*)malloc(sizeof(struct linked_list)); L->head = (Node*)malloc(sizeof(struct node)); L->head->prev = NULL; L->tail = (Node*)malloc(sizeof(struct node)); L->head->next = L->tail; L->tail->prev = L->head; L->tail->next = NULL; return L; } List* Insert(List* L, int value) { Node* ptr = (Node*)malloc(sizeof(struct node));// allocate a piece of memory for the new node ptr->v = value; Node* current = L->head; while (1) { if (current->next == L->tail || value < current->next->v) { ptr->next = current->next; ptr->prev = current; current->next->prev = ptr; current->next = ptr; break; } else current = current->next; } return L; } List* Intersection(List* ListA, List* ListB) { //Generate a linked list head to contain the intersection of A and B in ascending order. //You can only visit the input linked list ListA and ListB but not edit them. Finally, return the intersection list. List* ListC = Create_List(); return ListC; } List* Union(List* ListA, List* ListB) { //Generate a linked list head to contain the union of A and B in ascending order. //You can only visit the input linked list ListA and ListB but not edit them. Finally, return the union list. List* ListC = Create_List(); return ListC; } void Print(List* L) { //do not forget to print a ' ' return; } int main() { List* ListA = Create_List(); List* ListB = Create_List(); int n, m; int value; printf("Input the number of integers in A: "); scanf("%d", &n); printf("Input these integers: "); for (int i = 0; i < n; i++) { scanf("%d", &value); ListA = Insert(ListA, value); } printf("Input the number of integers in B: "); scanf("%d", &m); printf("Input these integers: "); for (int i = 0; i < m; i++) { scanf("%d", &value); ListB = Insert(ListB, value); } List* ListC = NULL; List* ListD = NULL; ListC = Intersection(ListA, ListB); ListD = Union(ListA, ListB); Print(ListA); Print(ListB); printf("The intersection list contains: "); Print(ListC); printf("The union list contains: "); Print(ListD); return 0; } -------------------------------------------End of Code-------------------------------------------
Related Book For
Computer Organization and Design The Hardware Software Interface
ISBN: 978-0124077263
5th edition
Authors: David A. Patterson, John L. Hennessy
Posted Date: