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>//preprocessor directive
#include <stdlib.h>
typedef struct node//creating a list
{
int data;//members of a structure
struct node *next;
}Node;
static Node *head1,*head2,*newnode1,*newnode2,*head3,*newnode3,*head4,*newnode4;//declaration for creation and insertion
int *insertion2(Node *head2,int num);//function for insertion
int *insertion1(Node *head1,int num);
int uni(Node *hf,int num);//to find union and intersection between lists
int main()
{
int N1=0,N2=0;//for number of nodes
printf("\n enter the number of nodes for list 1::");
scanf("%d",&N1);
for(int i=0;i<N1;i++)//nodes for list 1
{
int num=0;
printf("\n enter the number::");
scanf("%d",&num);//get the value from user
head1=insertion1(head1,num);//creation list 1
}
printf("\n enter the number of nodes for list 2::");
scanf("%d",&N2);
for(int i=0;i<N2;i++)//creating node for list 2
{
int num=0;
printf("\n enter the number for list 2::");
scanf("%d",&num);//get the value from user
head2=insertion2(head2,num);//creation list 2
}
unionoflist(head1,head2);//function call for to get the union between list 1 and list 2
intersectionlist(head1,head2);//function call for to get the intersection between list 1 and list 2
}
int *insertion1(Node *head1,int num)//insertion nodes for link list 1
{
newnode1=(Node*)malloc(sizeof(Node));
newnode1->data=num;
newnode1->next=head1;
head1=newnode1;
return head1;//returning to connect between nodes
}
int *insertion2(Node *head2,int num)//insertion nodes for link list 2
{
newnode2=(Node*)malloc(sizeof(Node));
newnode2->data=num;
newnode2->next=head2;
head2=newnode2;
return head2;
}
int *insertion3(Node *head3,int num)//insertion nodes for link list 3
{
newnode3=(Node*)malloc(sizeof(Node));
newnode3->data=num;
newnode3->next=head3;
head3=newnode3;
return head3;
}
int *insertion4(Node *head4,int num)//insertion nodes for link list 4
{
newnode4=(Node*)malloc(sizeof(Node));
newnode4->data=num;
newnode4->next=head4;
head4=newnode4;
return head4;
}
int unionoflist(Node *head1,Node *head2)//function for to find union between list 1 and list 2
{
Node *LL1=head1,*LL2=head2;
while(LL1!=0)
{
head3=insertion3(head3,LL1->data);
LL1=LL1->next;//to change position of node
}
while(LL2!=0)
{
if(!uni(LL1,LL2->data))
{
head3=insertion3(head3,LL2->data);//insertion of selected nodes
}
LL2=LL2->next;//to change position of node
}
sorting(head3);//sorting before printing
Node *LL3=head3;
printf("\n The union between list 1 and list 2::");
while(LL3!=0)
{
printf(" %d",LL3->data);//printing link list 3
LL3=LL3->next;//to change position of node
}
}
int uni(Node *hf,int data)
{
hf=head1;
while(hf!=0)
{
if(hf->data==data)//checking if data matches return 1 else didn't
{
return 1;
}
hf=hf->next;//to change position of node
}
}
int intersectionlist(Node *head1,Node *head2)//function for to find intersection between list 1 and list 2
{
Node *LL1=head1,*LL2=head2;
while(LL2!=0)
{
if(uni(LL1,LL2->data))
{
head4=insertion4(head4,LL2->data);//insertion of selected nodes
}
LL2=LL2->next;//to change position of node
}
sorting(head4);//sorting before printing
Node *LL4=head4;
printf("\n The intersection between list 1 and list 2::");
while(LL4!=0)
{
printf(" %d",LL4->data);//printing link list 4
LL4=LL4->next;//to change position of node
}
}
void sorting(Node *hf)
{
Node *t1,*next1;//creating objects
t1=hf;
while(t1!=NULL)
{
next1=t1->next;
while(next1 !=NULL)//sorting concept using temporary variable
{
if((t1->data)>(next1->data))
{
int temp=t1->data;
t1->data=next1->data;
next1->data=temp;
}
next1=next1->next;//incrementing
}
t1=t1->next;//incrementing
}
}
OUTPUT::
enter the number of nodes for list 1::4
enter the number::1
enter the number::2
enter the number::3
enter the number::4
enter the number of nodes for list 2::4
enter the number for list 2::3
enter the number for list 2::4
enter the number for list 2::5
enter the number for list 2::6
The union between list 1 and list 2:: 1 2 3 4 5 6
The intersection between list 1 and list 2:: 3 4