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:

We use c programing language for make mathematical set theory "Union" and "Intersection" oparetion.

Process:

1.Initialige bthe header file for make link list related function.

Here we use "stdio.h" ," stdlib.h" and "stdbool.h" header file.

2.Then we make a structure for our useable node.

3.Then we create push function for input the list elimant into the list.

4.Then we make a IS_in function for compareing two list elimant for comparing .

5.Then we make getUnion and getIntersection funtion for perform the union and intersection oparetion.

6.We make printdata function for print or display the result.

7.After end of the program we use main function for emplyes the total algaritham.

Math oparetion Union:

In the mathematics set theory there are two set set A and set B ,now meaning of union means the eliment which set of elimants for both are called union set.

like,

A=[4,5,6,8]

B=[5,6,9,2]

now Union set =[4,5,6,9,2]

Math oparetion Intersection:

In the mathematical set theory there are two set A and B , Now meaning of Inter section is set of common eliment between A and B.

like,

A=[4,5,6,8]

B=[5,6,9,2]

now Intersection set =[5,6]

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

//Creat a node structure,

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

//Create a push function for input or put the pointer eliment,

void push(struct node** head,int new_data)
{
    struct node* new_node=(struct node*)malloc(sizeof(struct node));
    new_node->data=new_data;
    new_node->next=(*head);
    (*head)=new_node;    
}

//Create a IS_in function for comparing two nodes eliment,

bool IS_in(struct node* new_nod,int data)
{
   struct node* t=new_nod;
   while (t!=NULL)
   {
      if (t->data==data)
      {
        return 1; 
      }   
     t=t->next; 
   }
   return 0;
}

//Create a getUnion function to performe union oparation between two node,

struct node* getUnion(struct node* head1,struct node* head2)
{ 
   struct node* result=NULL;
   struct node* t1=head1,*t2=head2;
   while (t1!=NULL)
   {
       push(&result,t1->data);
       t1=t1->next;
   }
   while (t2!=NULL)
   {
     if (!IS_in(result,t2->data))
     {
        push(&result,t2->data); 
     } 
     t2=t2->next;    
   }
   return result;
}

//Create a getIntersection function to performe Intrsection oparation between two node,

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

//Create printdata function for print or display the result,

int printdata(struct node* nod)
{
    while(nod!=NULL)
    {
        printf("%d ",nod->data);
        nod=nod->next;
    }
}

//Main function

int main()
{
    struct node* head1=NULL;
    struct node* head2=NULL;
    struct node* unin=NULL;
    struct node* Intersec=NULL;

    //Create the Head1 linklist 

    push(&head1,20);
    push(&head1,50);
    push(&head1,2);
    push(&head1,7);
    push(&head1,6);
    push(&head1,5);
    push(&head1,4);

    //Create the Head2 linklist 

    push(&head2,9);
    push(&head2,7);
    push(&head2,6);
    push(&head2,50);
    push(&head2,46);
    push(&head2,10);
    push(&head2,1);

    //Display the nodes and oparetion result linklist,

    printf("1st list is:\n");
    printdata(head1);
    printf("\n2nd list is:\n");
    printdata(head2);
    
    unin=getUnion(head1,head2);
    Intersec=getIntersection(head1,head2);
    printf("\n After union oparetion the list is:\n");
    printdata(unin);
    printf("\n After Intrsection oparetion the list is:\n");
    printdata(Intersec);
}

In our quistion we use two list like list1=4,5,6,7,2,50,20 and list2=1,10,46,50,6,7,9

After performing the oparetion we get union set is :9,46,10,1,20,50,2,7,6,5,4.

and Intersection set is: 7,6,50

,

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.