Linked Lists Sum To K 2022 (Required)


Time limit: 2000ms
Memory limit: 256mb

Description:

In this exercise, we use the singly linked lists to maintain two sequences of distinct integers, one in ascending order (denoted as linked list A) and one in descending order (denoted as linked list B). Given a positive integer k, we may pick one integer a from A, and one integer b from B, such that a + b = k. In this problem, you are required to find out how many such pairs exist for the given k and linked lists A and B.

To keep the integers in ascending/descending 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.

The user will provide the (unsorted) integers of A and B, and a positive integer k. You are required to fill in the code for the following functions, in the positions indicated by the comment '// code here'.

1.    Insert the numbers in ascending or descending orders.
List Insert(List head, int value, int asc);
- This function handles both insertion in ascending and descending order, depending on the value of asc.

2.    Compute the number of pairs of intergers, one from A and one from B, such that their sum is k.
int NumberOfSumToK(List headA, List headB, int k); 
- You can only visit the input linked list headA and headB but not change them. Finally, return the required number of pairs.

3.    Print all integers of the linked list in order.
void Print(List head);
- 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
10

Sample Output 1:
Input the number of integers in A:
Input these integers:
Input the number of integers in B:
Input these integers:
Input k:
1 4 7 10
11 7 4 3 2
Number of pairs summed to k:
1


Sample Input 2:
3
1 0 2 
3
0 -1 2
1

Sample Output 2:
Input the number of integers in A:
Input these integers:
Input the number of integers in B:
Input these integers:
Input k:
0 1 2
2 0 -1
Number of pairs summed to k:
2


-------------------------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, int asc)
{
    Node *ptr = (List)malloc(sizeof(Node)); // allocate a piece of memory for the new node
    ptr->value = value;
    if(head==NULL || value < head->value && asc == 1 || value > head->value && asc == 0)
    {
        ptr->next = head;
        return ptr;
    }
    Node *current = head;
    while(1)
    {
        if(asc == 1){ // ascending order
            // code here
            
        }
        else if(asc == 0){ // descending order
            // code here
            
        }
    }
    return head;
}

int NumberOfPairsSumToK(List headA, List headB, int k)
{
    int numberOfPairs = 0;
    // code here
    
}

void Print(List head)
{
    // do not forget to print a '\n'
    // code here
    
}

int main()
{
    List headA = NULL;
    List headB = NULL;
    int n, m, k;
    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, 1);
    }

    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, 0);
    }
    printf("Input k:\n");
    scanf("%d", &k);

    int noOfPairs;
    noOfPairs = NumberOfPairsSumToK(headA, headB, k);

    Print(headA);
    Print(headB);
    printf("Number of pairs summed to k:\n");
    printf("%d\n", noOfPairs);
    return 0;
}





-------------------------------------------End of Code-------------------------------------------

Submit