Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
union & intersection of two linked list
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node {
int data;
struct node* next;
};
void display1(struct node *start1);//display the data of start 1 lined list
struct node*create1(struct node*start1);
void display2(struct node *start2);//display the data of start 2 lined list
struct node*create2(struct node*start2);
struct node *intersect(struct node *start1,struct node *start2);//prototype function of intersection of two lnked
struct node *merge(struct node *start1,struct node *start2);77prototype function of merging of two lnked
void Union(struct node *start1,struct node *start2);//union of two linked list
int main()//main function
{
struct node *start1,*start2;
start1=NULL;
start2=NULL;
//fuction calling//
start1=create1(start1);
display1(start1);
start2=create2(start2);
display2(start2);
intersect(start1,start2);
merge(start1,start2);
printf("after merging\n");
display1(start1);
printf("\nunion\n");
Union(start1,start2);
}
struct node* create1(struct node *start1)
{
struct node *temp1,*nn1;
int n1,i;
printf("enter length of linked list1");
fflush(stdout);
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
//creating memory for linked list 1
nn1=(struct node*)malloc(sizeof(struct node));
fflush(stdout);
scanf("%d",&nn1->data);
nn1->next=NULL;
if(start1==NULL)
{
temp1=start1=nn1;
}
else
{
temp1->next=nn1;
temp1=temp1->next;
}
}
return(start1);
}
void display1(struct node *start2)
{
struct node *temp2;
temp2=start2;
while(temp2!=NULL)
{
printf("%d->",temp2->data);
temp2=temp2->next;
}
}
struct node* create2(struct node *start2)
{
struct node *temp2,*nn2;
int n2,j;
printf("enter length of linked list2");
fflush(stdout);
scanf("%d",&n2);
for(j=0;j<n2;j++)
{
//creating memory for linked list 2
nn2=(struct node*)malloc(sizeof(struct node));
fflush(stdout);
scanf("%d",&nn2->data);
nn2->next=NULL;
if(start2==NULL)
{
temp2=start2=nn2;
}
else
{
temp2->next=nn2;
temp2=temp2->next;
}
}
return(start2);
}
void display2(struct node *start2)
{
struct node *temp2;
temp2=start2;
while(temp2!=NULL)
{
printf("%d->",temp2->data);
fflush(stdout);
temp2=temp2->next;
}
}
function definition of intersection of two linked list//
struct node *intersect(struct node *start1,struct node *start2)
{
struct node *x,*y;
for( x=start1;x!=NULL;x=x->next)
{
for( y=start2;y!=NULL;y=y->next)
{
if(x->data==y->data)
{
printf("intersect of 2 linked list");
fflush(stdout);
printf("\n%d\n",x->data);
break;
}
}
}
return(start1);
}
//function definition of mergring two linked list//
struct node *merge(struct node *start1,struct node *start2)
{
struct node *temp;
temp=start1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=start2;
return(start1);
}
function definition of sorting of two linked list//
struct node *sort(struct node *start1,struct node *start2)
{
struct node *a,*b;
int temp ;
for( a=start1;a!=NULL;a=a->next)
{
for( b=a->next;b!=NULL;b=b->next)
{
if(a->data > b->data)
{
temp=a->data;
a->data=b->data;
b->data=temp;
}
}
}
return (start1);
}
//function definition of union of two linked list//
void Union(struct node *start1,struct node *start2)
{
struct node *i,*j;
i=start1;
j=start2;
for(i=start1;i!=NULL;i=i->next)
{
for(j=i->next;j!=NULL;j=j->next)
{
if(j->next==NULL)
{
printf("%d,",j->data);
}
if(i->data==j->data)
{
break;
}
else
{
printf("%d,",i->data);
break;
}
}
}
}
OUTPUT Of two linked list
enter length of linked list1:-3
5
1
7
5->1->7->enter length of linked list2:-3
7
9
8
7->9->8->
intersect of 2 linked list:-
7
after merging
5->1->7->7->9->8->
union of two linked list
5,1,7,8,9,