Merge Two Linked Lists
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 '\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 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 '\n'
return;
}
int main()
{
List* ListA = Create_List();
List* ListB = Create_List();
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);
ListA = Insert(ListA, 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);
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:\n");
Print(ListC);
printf("The union list contains:\n");
Print(ListD);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit