Merge Two Linked Lists (Required)
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 '\n' 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-------------------------
/*
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 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<head->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 '\n'
}
int main()
{
List headA = NULL;
List headB = NULL;
int n, m;
int value;
printf("Input the number of integers in A:\n");
scanf("%d", &n);
printf("Input these integers:\n");
for(int i=0; i<n; i++)
{
scanf("%d", &value);
headA = Insert(headA, value);
}
printf("Input the number of integers in B:\n");
scanf("%d", &m);
printf("Input these integers:\n");
for(int i=0; i<m; i++)
{
scanf("%d", &value);
headB = Insert(headB, value);
}
List headC = NULL;
List headD = NULL;
headC = Intersection(headA, headB);
headD = Union(headA, headB);
Print(headA);
Print(headB);
printf("The intersection list contains:\n");
Print(headC);
printf("The union list contains:\n");
Print(headD);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit