Search code examples
cpointersdata-structureslinked-listsingly-linked-list

Finding the intersection and union of 2 singly linked list


The program generates the union and intersection of 2 linked lists. However, it does not generate the expected output where duplicate values from list 1 and list 2 should not be present and the intersection list is not even being displayed. How can I resolve this issue?

`

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
    struct node *next;
} node;

void insertAtBeg(node **head, int ele){
    node *newnode = (node*)malloc(sizeof(node));
    newnode->data = ele;
    newnode->next = (*head);
    (*head) = newnode;
}

int isPresent(node *temp, int ele){
    node *t = temp;
    while(t != NULL){
        if(t->data == ele)
            return 1;
        t = t->next;
    }
    return 0;
}

void printList(node *n){
    while(n != NULL){
        printf("%d->",n->data);
        n = n->next;
    }
}

node* getUnion(node *head1,node *head2){
    node *result = NULL;
    node *t1 = head1;
    node *t2 = head2;
    while(t1 != NULL){
        insertAtBeg(&result, t1->data);
        t1 = t1->next;
    }

    while(t2!=NULL){
        if(!isPresent(result, t2->data));
            insertAtBeg(&result,t2->data);
        t2 = t2->next;
    }
    return result;
}

node *getIntersection(node *head1,node *head2){
    node* result = NULL;
    node* t1 = head1;
    while(t1 != NULL){
        if(isPresent(head2, t1->data))
            insertAtBeg(&result,t1->data);
        t1 = t1->next;
    }
    return result;
}

int main(){
    node *intersection = NULL;
    node *unin = NULL;
    node *List1;
    node *List2;
    List1 = List2 = NULL;
    int i,n,m,temp;
    
    printf("Enter the size of the first linked list:\n");
    scanf("%d",&n);
    printf("Enter %d elements\n",n);
    for(i = 0;i < n;i++){
        scanf("%d",&temp);
        insertAtBeg(&List1,temp);
    }
     printf("Displaying list 1:\n");
     printList(List1);

    printf("\nEnter the size of the second linked list:\n");
    scanf("%d",&m);
    printf("Enter %d elements\n",m);
    for(i = 0;i < m;i++){
        scanf("%d",&temp);
        insertAtBeg(&List2,temp);
    }
    printf("Displaying list 2:\n");
    printList(List2);

    unin = getUnion(List1,List2);
    intersection = getIntersection(List1,List2);

    printf("\nLinked List with Union of List1 and List2:\n");
    printList(unin);
    printf("\nLinked List with Intersection of List1 and List2:\n");
    printList(intersection);
    return 0;
}

`

Generated output: Enter the size of the first linked list: 4 Enter 4 elements 3 4 6 1 Displaying list 1: 1->6->4->3-> Enter the size of the second linked list: 4 Enter 4 elements 8 9 10 1 Displaying list 2: 1->10->9->8-> Linked List with Union of List1 and List2: 8->9->10->1->3->4->6->1-> Linked List with Intersection of List1 and List2: 1->


Solution

  • Within the function getUnion there is a typo

    while(t2!=NULL){
        if(!isPresent(result, t2->data));
                                       ^^^^
            insertAtBeg(&result,t2->data);
        t2 = t2->next;
    }
    

    Remove the semicolon.

    Also after this call

    printList(intersection);  
    

    output the new line character like for example

    putchar( '\n' );
    

    Pay attention to that you should declare parameters that denotes pointers to nodes with qualifier const if lists are not changed within functions as for example

    int isPresent( const node *temp, int ele){
        const node *t = temp;
        while(t != NULL){
            if(t->data == ele)
                return 1;
            t = t->next;
        }
        return 0;
    }
    
    void printList(const node *n){
        while(n != NULL){
            printf("%d->",n->data);
            n = n->next;
        }
    }
    
    node* getUnion( const node *head1,const node *head2){
        node *result = NULL;
        const node *t1 = head1;
        const node *t2 = head2;
        while(t1 != NULL){
            insertAtBeg(&result, t1->data);
            t1 = t1->next;
        }
    
        while(t2!=NULL){
            if(!isPresent(result, t2->data))
                insertAtBeg(&result,t2->data);
            t2 = t2->next;
        }
        return result;
    }
    
    node *getIntersection(const node *head1,const node *head2){
        node* result = NULL;
        const node* t1 = head1;
        while(t1 != NULL){
            if(isPresent(head2, t1->data))
                insertAtBeg(&result,t1->data);
            t1 = t1->next;
        }
        return result;
    }