Challenge

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

#include<stdio.h>
#include<stdlib.h>
typedef struct node // data of type struct
{
int data;
struct node *next;
}Node;

Node *setA=NULL;
Node *setB=NULL;
Node *tail;
Node *head_intersection=NULL;
Node *head_union;
//Node *tail;
Node * insert(int x,Node *set_ref) //function for creation
{
Node *temp;
temp=set_ref;
temp=(Node*)malloc(sizeof(Node));
temp->data=x;
temp->next=set_ref;
set_ref=temp;
return (set_ref);
}
void display(Node *set_ref) //function for display
{
Node *ptr;
ptr=set_ref;
while(ptr!=NULL)
{ printf("%d\t",ptr->data);
ptr=ptr->next;
}
tail=ptr;

}
int compare(Node *set_ref ,int data)
{
while(set_ref!=NULL)
{
if(set_ref->data==data)
return 1;
set_ref=set_ref->next;
}
return 0;
}

Node *intersection(Node * setA,Node *setB) //set A intersection set B
{
while(setA!=NULL)
{
if (compare(setB,setA->data))
head_intersection=insert(setA->data,head_intersection);
setA=setA->next;
}
return head_intersection;
}

Node *getunion(Node * setA,Node *setB) //set A union set B
{
while(setA!=0)
{
head_union=insert(setA->data,head_union);
setA=setA->next;
}
while(setB!=0)
{ if(!compare(head_union,setB->data))
head_union=insert(setB->data,head_union);
setB=setB->next;
}
return(head_union);
}

int main() //Main programme
{

int listA,elements_listA,listB,elements_listB;
printf("\n Enter the SET A list quantity\t");
scanf("%d",&listA);
printf("Enter the %d elements of listA\t ",listA);
for (int i=0;i<listA;i++)
{ scanf("%d",&elements_listA);
setA=insert(elements_listA,setA);

}


display(setA);
printf("\n Enter the SET B list quantity\t");
scanf("%d",&listB);
printf("Enter the %d elements of listA\t",listB);
for (int i=0;i<listB;i++)
{

scanf("%d",&elements_listB);
setB=insert(elements_listB,setB);

}
display(setB);
getunion(setA,setB);
printf("\n Union of set A and Set B :\t");
display(head_union);
intersection(setA,setB);
printf("\n Intersection of set A and Set B :\t");
if(head_intersection==NULL) printf("\n Intersection is nil");
display(head_intersection);

}

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.