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//standard input output library for C language
#include//standard library for number conversion,storage allocation
#include//data type that contains two types of values, i.e., 0 and 1
struct node{ //linked list node
int data;
struct node *nxt;
};
bool present(struct node * head,int val);//function to check if given data is present in a list
void push(struct node **head,int val);//function to insert node at the beginning of a linked list
struct node *uni(struct node *head1,struct node *head2)//function to get union of 2 linked list
{
struct node *result=NULL;
struct node *t1=head1;
struct node *t2=head2;
while(t1!=NULL) //inserts all elements present in list1 to result list
{
push(&result,t1->data);
t1=t1->nxt;
}
while(t2!=NULL)//inserts those elements of list2 that are not present in result list
{
if(!present(result,t2->data))
{
push(&result,t2->data);
}
t2=t2->nxt;
}
return result;//returns result to called function
}
struct node * inter(struct node * head1,struct node * head2)//function to get intersection of list1 and list2
{
struct node *t1=head1;
struct node *result=NULL;
while(t1!=NULL)//Traverse list1 and search each element of it in list2
{
if(present(head2,t1->data))//if element is present in list2,then it inserts element in result list
{
push(&result,t1->data);
}
t1=t1->nxt;
}
return result;//returns result to called function
}
bool present(struct node * head,int val)//function to check if given data is present in a list
{
struct node * t1=head;//if element is present in linked list ,it returns true or else false
while(t1!=NULL)
{
if(t1->data==val)//compares whether the data present in list is equal with given value
{
return 1;//if true ,it returns 1
}
t1=t1->nxt;
}
return 0;// to exit from function
}
void push(struct node **head,int val)
{
struct node * newval=(struct node*)malloc(sizeof(struct node));//to allocate memory for new node
newval->data=val;
newval->nxt=*head;
*head=newval;
}
display(struct node *head)//function to print the linked list
{
while(head!=NULL)
{
printf(" %d",head->data);
head=head->nxt;
}
}
int main()//main function
{
struct node * head1=NULL;//Starts with the empty list
struct node * head2=NULL;
struct node* unio=NULL;
struct node * intersection=NULL;
int l,o,val;

l=1;

while(l==1)
{
printf("\n Enter 0 to add values in list1 or 1 to add values in list2");
fflush(NULL);
scanf("%d",&o);
if(o==0)
{
printf("\n Enter value for list 1");//to add elements for list 1
fflush(NULL);
scanf("%d",&val);
push(&head1,val);//used to add elements at the beginning of linked list
}
if(o==1)
{
printf("\n Enter value for list 2");// to add elements for list 2
fflush(NULL);
scanf("%d",&val);
push(&head2,val);
}
printf("enter 0 to exit or 1 to continue");
fflush(NULL);
scanf("%d",&l);
}
intersection=inter(head1,head2);//funtion to check intersection of 2 linked list
unio=uni(head1,head2);//function to check union elements of 2 linked list
printf("\n First linked list is \n");//displays first linked list
display(head1);
printf("\n Second linked list is \n");//displays second linked list
display(head2);
printf("\n Intersection list is \n");//displays list containing intersection of 2 linked list
display(intersection);
printf("\n Union list is \n");//displays list containing union of 2 linked list
display(unio);
return 0;
}

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.