Question: Question related to Data structure (C language), thanks. Time limit: 2000ms Memory limit: 256mb Description: In this exercise, we use the singly linked list to
Question related to Data structure (C language), thanks. Time limit: 2000ms Memory limit: 256mb Description: In this exercise, we use the singly 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.
-
#include#include typedef struct linked_list_node{ int value; struct linked_list_node* next; } Node; typedef Node* List; List Insert(List head, int value) { Node *ptr = (List)malloc(sizeof(Node)); // allocate a piece of memory for the new node ptr->value = value; if(head==NULL || value value) { ptr->next = head; return ptr; } Node *current = head; while(1) { if(current->next==NULL || valuenext->value) { ptr->next = current->next; current->next = ptr; break; } else current = current->next; } return head; } 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 edit them. Finally, return head. List head = NULL; 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 edit them. Finally, return head. List head = NULL; return head; } void Print(List head) { //do not forget to print a ' ' } int main() { List headA = NULL; List headB = NULL; 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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
