Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*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>
struct node
{
int data;
struct node *address;
};
//Set pointers declaration
struct node *set1;
struct node *set2;
struct node * uni;
struct node * inter;
//function prototypes
int createlist(int,int);
void display(int);
int unionlist(int,int);
int intersectionlist(int,int);
int main()
{
int n1,n2,n3,n4,n;
printf("\nplease enter number of nodes required for set1 list: ");
fflush(stdout);
scanf("%d",&n1);
if(n1>0)
{
n=createlist(n1,1); //creating set 1
printf("\nData in set1 list:\n");
display(1); //displaying set 1
}
else if(n1==0)
{
printf("\nset1 is empty list");
}
else
{
printf("\nwrong choice"); //if number of nodes is a negative number
return 0; // exiting unconditionally
}
printf("\nplease enter number of nodes required for set 2 list: ");
fflush(stdout);
scanf("%d",&n2);
if(n2>0)
{
n=createlist(n2,2);
printf("\nData in set2 list:\n");
display(2);
}
else if(n2==0)
{
printf("\nset2 is empty list");
}
else
{
printf("\nwrong choice"); //if number of nodes is a negative number
return 0; // exiting unconditionally
}
n3=unionlist(n1,n2);
printf("\nunion of set 1 and set 2 is: \n");
if((n2)==0)
{
display(1); // displaying set 1 as union set
}
else if((n1)==0)
{
display(2); //displaying set 2 as union set
}
else
{
display(3); // displaying union set
}
n4=intersectionlist(n1,n2);
printf("\nintersection of set 1 and set 2 is: \n");
if((n4)==0)
{
printf("\nnull set");
}
else
{
display(4); //displaying intersection set
}
return 0;
}
int createlist(int n,int i)
{
struct node *tmp1,*tmp2;
int num,j;
if(n==0)
{
return 0;
}
else
{
if(i==1)
{
set1=(struct node *)malloc(sizeof(struct node)); //allocating memory for set 1 list
if(set1==NULL)
{
printf("\nmemory not allocated");
}
else
{
printf("\n enter node 1 data of set1: ");
fflush(stdout);
scanf("%d",&num); // scaning element 1
set1->data=num;
set1->address=NULL;
tmp1=set1;
for(j=2;j<=n;j++)
{
tmp2=(struct node *)malloc(sizeof(struct node));
if(tmp2==NULL)
{
printf("\n memory not allocated");
}
else
{
printf("\nenter node %d data of set1: ",j);
fflush(stdout);
scanf("%d",&num); //scanning elements one at a time and storing them one at a time
tmp2->data=num;
tmp2->address=NULL;
tmp1->address=tmp2;
tmp1=tmp2; //for moving to next location
}
}
}
}
if(i==2)
{
set2=(struct node *)malloc(sizeof(struct node));
if(set2==NULL)
{
printf("\nmemory not allocated");
}
else
{
printf("\n enter node 1 data of set2: ");
fflush(stdout);
scanf("%d",&num); // scaning element 1
set2->data=num;
set2->address=NULL;
tmp1=set2;
for(j=2;j<=n;j++)
{
tmp2=(struct node *)malloc(sizeof(struct node));
if(tmp2==NULL)
{
printf("\n memory not allocated");
}
else
{
printf("\nenter node %d data of set2: ",j);
fflush(stdout);
scanf("%d",&num); //scanning elements one at a time and storing them one at a time
tmp2->data=num;
tmp2->address=NULL;
tmp1->address=tmp2;
tmp1=tmp2; //for moving to next location
}
}
}
}
}
return n;
}
void display(int c)
{
struct node *tmp3;
switch(c) // for selecting which list to display
{
case 1:
tmp3=set1;
break;
case 2:
tmp3=set2;
break;
case 3:
tmp3=uni;
break;
case 4:
tmp3=inter;
break;
}
while(tmp3!=NULL) //continue the loop untill last element
{
printf("\t%d",tmp3->data);
tmp3=tmp3->address; //for moving to next element
}
}
int unionlist(int n1,int n2)
{
struct node *tmp4,*tmp5,*tmp6,*tmp7,*rear;
int i,flag,c=0;
if(n1==0)
{
return n2; //if set 1 is a empty list
}
else if(n2==0)
{
return n1; //if set 2 is a empty list
}
else
{
uni=(struct node *)malloc(sizeof(struct node)); //allocating memory for union list
if(uni==NULL)
{
printf("\nmemory not allocated");
}
else
{
uni->data=set1->data; //coppying 1st elment of set 1 to union 1st element
uni->address=set1->address;
tmp4=uni;
for(i=2;i<=n1;i++)
{
tmp5=(struct node *)malloc(sizeof(struct node));
if(tmp5==NULL)
{
printf("\nmemory not allocated");
}
else
{
tmp5->data=tmp4->data; // copying elements of set 1 one at a time
tmp5->address=tmp4->address;
tmp4=tmp4->address;
if(i==(n1))
{
rear=tmp4; //for storing the last position address
}
}
}
tmp6=set2; //pointer for set 2
for(i=1;i<=n2;i++)
{
flag=0;
tmp4=uni;
while(tmp4!=NULL)
{
if((tmp4->data)==(tmp6->data)) //checking whether the element in set 1 and set 2 are same
{
flag=1;
break; //if the element is same breaking the loop to avoid repetition of elements stored in union
}
tmp4=tmp4->address;
}
if(flag==0)
{
tmp4=rear; //union pointer from recent last position
tmp7=(struct node *)malloc(sizeof(struct node));
if(tmp7==NULL)
{
printf("\nmemory not allocated");
}
else
{
tmp7->data=tmp6->data; //if the element is not repeated then copying it from set 2 to union list
tmp7->address=NULL;
tmp4->address=tmp7;
rear=tmp4->address;
c=c+1; //counting number of elements that are getting added from set 2 to union
}
}
tmp6=tmp6->address;
}
}
}
return ((n1)+c); //returning total elements in union
}
int intersectionlist(int n1,int n2)
{
struct node *tmp8,*tmp9,*tmp10,*tmp11;
int i,flag,j,c,k;
if((n1)==0 || (n2)==0) //if either of the set is null set then making intersection also null set
{
return 0;
}
else
{
i=0;
tmp8=set1;
for(j=0;j<n1;j++) /*here two loops works in such a way that one element of set 1 gets checked with all elements of set 2
and process continues till all elements of set 1 are completed */
{
flag=0;
tmp9=set2;
for(k=0;k<n2;k++)
{
if((tmp8->data)==(tmp9->data))
{
flag=1; //here flag says about elements getting matched or not
i=i+1; //where as i says about the number of elements getting matched in such manner
break;
}
tmp9=tmp9->address;
}
if((flag==1) && (i==1))
{
inter=(struct node *)malloc(sizeof(struct node));
if(inter==NULL)
{
printf("\nmemory not allocated");
}
else
{
inter->data=tmp8->data;
inter->address=NULL;
tmp10=inter;
c=1;
}
}
if((flag==1) && (i>=2))
{
tmp11=(struct node *)malloc(sizeof(struct node));
if(tmp11==NULL)
{
printf("\nmemory not allocated");
}
else
{
tmp11->data=tmp8->data;
tmp11->address=NULL;
tmp10->address=tmp11;
tmp10=tmp11;
c=c+1;
}
}
tmp8=tmp8->address;
}
}
return (c);
}