Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

ANS:

#include

//We have to include stdio.h (standard input output library for C language)

#include

//We have to include stdlib.h (standard library for memory allocation)

#include

//We have to include string.h (for operating string operations)

//creating a structure &name of the structure is: node

struct node

{

    int data;                           //int is the data type & data is the structure member

    struct node* next;                 //struct node is a poineter which contains address & points to the next node

}*list1=NULL,*list2=NULL,*uni=NULL,*inter=NULL;//creating all the variables with initailize to null

void insert(struct node **head,int value) //create double pointer

{

    struct node *local=(struct node*)malloc(sizeof(struct node)); //memory allocation of struct node using malloc

    if(local == NULL)                                  //if the local is to null the print memory not allocated

{

    printf("memory is not allocated:");          //prints not allocated memory

} 

else                                         //if cndition fails to operate then else will execute

{

local->data=value;                         //assign the data to value

    local->next=*head;                        //it assigns the value of the node list-1

    *head=local;                             //local value stores the value of head

}                          

}

void disp(struct node *head)           //create display function

{

    while(head!=NULL)                      //the dispaly is running until head is not equal to null

    {

   printf("%d\n",head->data);         //print the value of head

       head=head->next;                   //head points the next

    }

}

int compare(struct node *list, int data) //create a structure to compare the values

{

    while(list != NULL)                       //it compares the values until list is equal to null

    {

    if(list-> data == data)             //comparing values

        {

            return 1;                      //if both the values are equal it returns-1

        }

        list = list->next;                  //now the list is assign to next

    }

    return 0;

}

/*function to get the union point of two linked  lists L1 and L2 */

    struct node *get_uni(struct node *L1, struct node *L2)  //create a structure of union  with l1 and l2

    {

    struct node *temp = L1;                        //creating a variable called temp & assign the list-1 to it

    while(temp != NULL)                           //insertion node is run until temp is not equal to null

    {

        insert(&uni,  temp->data);              //insert the elments to the newly created list union

        temp = temp->next;                      //temp assign the next element

    }

    while(L2 != NULL)                          //while l2 is not equal null the loop get run

    {

        if(!compare(L1, L2->data))            //comparing the l1 to the value of l2 data

        {

            insert(&uni, L2->data );         //insert(union) the elements or data to list-2

        }

        L2 = L2->next;                         //now the list is assign to next

    }

    return uni;                      //after the completion of process it returns the union elements

    }

    /*function to get the intersection point of two linked lists L1 and L2 */

    struct node *get_inter(struct node *L1, struct node *L2)   //create a structure of intersection with pointer of l1 and l2

    {

    while(L1 != NULL)                                         //insertion node is run until l1 is not equal to null

    {

        if(compare(L2,L1->data))                              //comparing the l2 to the value of l1

    {

    insert(&inter,L1->data);                                //insert(intersection) the elements or data to list-1

    }

    L1 = L1->next;                                         //L1 points to next

    }

    return inter;                         //after the completion of process it returns the intersection elements

    }

 int main()

//starting of main function,inside this main function we have to write our logic

    {

    int n1,n2,d1,d2;                             //initialize variables of n1,n2 and for elements d1&d2

    printf("Enter the list1 no.of elements: "); //enter the no.elements from the user(4) of list-1

    scanf("%d",&n1);                           //to read the elements from user of n1

    printf("\nEnter those elements: \n");        //enter the elemets 1,2,3,4

    for(int i=0;i

    {

        scanf("%d",&d1);                     //to read the data of list-1

        insert(&list1,d1);                   //insert the elments of data  of list-1

    }

    printf("\nthe entered elements: \n");       //enter the elements

    disp(list1);                         //display the list-1 elements

     printf("Enter the list2 no.of elements: "); //enter the no.elements from the user(4) of list-2

    scanf("%d",&n2);                            //to read the elements from user of n1

    printf("\nEnter those elements: \n");          //enter the elemets

    for(int i=0;i

    {

        scanf("%d",&d2);                       //to read

        insert(&list2,d2);                    //insert the elments of data  of list-2

    }

    printf("\nthe entered elements: \n");        //enter the elements

    disp(list2);                          //display the list-2 elements

    get_uni(list1,list2);                 //to get the union elements from l1 and l2

    printf("\nthe union elements are: \n");      //entered the union elements

    disp(uni);                             //display the union elements

    get_inter(list1,list2);  //to get the intersection elements from l1 and l2

    printf("\nthe intersection elements are: \n"); //entered the intersection elements

    disp(inter);                        //display the intersection elements

    return 0;

    }

OUTPUT:

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.