Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
1)link list;
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
//link list node
struct node
{
int data;
struct node*next;
};
//function declaration
void push(struct node**head_ref,int new_data);
int ispresent(struct node*head,int data);
//function to get union of two linked lists head1 and head2
struct node*getunion(struct node*head1,struct node*head2)
{
struct node*result=NULL;
struct node*t1=head1,*t2=head2;
//insert all elements of list1 to the result list
while(t1 !=NULL)
{
push(&result,t1->data);
t1=t1->next;
}
//insert those elements of list2 which are not present in result list
while(t2 !=NULL)
{
if(!ispresent(result,t2->data))
{
push(&result,t2->data);
}
t2=t2->next;
}
return result;
}
//function to get intersection of two linked lists head1 and head2
struct node*getintersection(struct node*head1,struct node*head2)
{
struct node*result=NULL;
struct node*t1=head1;
//traverse list1 and search each elements of in list2.if the element is present in list2,then insert the element to result
while(t1 !=NULL)
{
if (ispresent(head2,t1->data))
{
push(&result,t1->data);
}
t1=t1->next;
}
return result;
}
//a utility function to insert a node at the beginning of a linked list
void push(struct node**head_ref,int new_data)
{
//allocate node
struct node*new_node=(struct node*)malloc(sizeof(struct node));
//put in the data
new_node->data=new_data;
//link the old list off the new node
new_node->next=(*head_ref);
//move the head to point to the new node
(*head_ref)=new_node;
}
//function to print a linked list
void printlist(struct node*node)
{
while(node !=NULL)
{
printf("%d",node->data);
node=node->next;
}
}
//function that returns true if data is present in linked list else return false
int ispresent(struct node*head,int data)
{
struct node*t=head;
while(t !=NULL)
{
if(t->data==data)
{
return 1;
}
t=t->next;
}
return 0;
}
//drivers program to test above function
int main()
{
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d%d",&n,&m);
//start structure pointer with the empty list
struct node*head1=NULL;
struct node*head2=NULL;
struct node*intersecn=NULL;
struct node*unin=NULL;
//for loop for link list1
for(int i=0;i<n;i++)
{
printf("enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head1,temp);
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head2,temp);
}
//call the function of intersection
intersecn=getintersection(head1,head2);
//call the function of union
unin=getunion(head1,head2);
printf("\n first list is");
//function calling with structure pointer as argument
printlist(head1);
printf("\n second list is");
printlist(head2);
printf("\n intersection list is");
printlist(intersecn);
printf("\n union list is");
printlist(unin);
return 0;
}