Challenge

Implementation of Linked List

#include 
#include 

struct node                 //creation of node
{
    int data;
    struct node* next;
};

struct node* createnode(int data) 
{
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}

void insert(struct node** head, int data)  //Generalized function to insert elements in any given linkedlist 
{
    struct node* newnode = createnode(data);
    
    if (*head == NULL)                    // checking for empty list
	{
        *head = newnode;
        return;
	}
	
    struct node* temp = *head;           // traversing across the list to reach the end of list
    while (temp->next != NULL)
    {temp = temp->next;}
    
    temp->next = newnode;
}

int find(struct node* head, int data)  
{
    struct node* temp = head;
    while (temp != NULL) 
    {
        if (temp->data == data)         // if equal return the value 1
            return 1;
        temp = temp->next;
    }
    return 0;
}

struct node* unionset(struct node* head1, struct node* head2) {
    struct node* unionhead = NULL;
    struct node* temp1 = head1;
    while (temp1 != NULL)                  //inserting all the elements of set 1 
    {
        insert(&unionhead, temp1->data);
        temp1 = temp1->next;
    }
    struct node* temp2 = head2;
    while (temp2 != NULL) {
        if (!find(unionhead, temp2->data))  // the data of unionhead which has the set1 is now checked with data of set 2
            insert(&unionhead, temp2->data);// insertion is not made if the value is already present.
        temp2 = temp2->next;
    }
    return unionhead;  //returning the address of unionset linkedlist
}

struct node* intersectionSet(struct node* head1, struct node* head2) {
    struct node* intersection_head = NULL;
    struct node* temp = head1;

    while (temp != NULL) {
        if (find(head2, temp->data))       //inserting only the elements equal in both sets
            insert(&intersection_head, temp->data);
        temp = temp->next;
    }
    return intersection_head;  //returning the address of intersectionSet linkedlist
}



void printList(struct node* head)   // program to print the list
{
    struct node* temp = head;
    while (temp != NULL) 
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main() {
    struct node* head1 = NULL;
    struct node* head2 = NULL;
    int n1, n2, data;
    printf("Enter number of elements in set 1: ");
    scanf("%d", &n1);
    printf("Enter elements of set 1: ");
    
    for (int i = 0; i < n1; i++) 
    {
        scanf("%d", &data);
        insert(&head1, data);
    }
    
    printf("Enter number of elements in set 2: ");
    scanf("%d", &n2);
    printf("Enter elements of set 2: ");
    for (int i = 0; i < n2; i++) 
    {
        scanf("%d", &data);
        insert(&head2, data);
    }
    
    struct node* unionhead = unionset(head1, head2);
    printf("Union of sets: ");
    printList(unionhead);
    
    struct node* intersection_head = intersectionSet(head1, head2);
    printf("intersection of sets: ");
    printList(intersection_head);
    return 0;
}

Output:

C program File: 

https://drive.google.com/file/d/11sEOrZpsOmd51zrrJtHjcoWp8ZbIu7yp/view?usp=sharing

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.