Code:
#include<stdio.h>
#include<conio.h>
#include "SET.H"
void Set_Create(Set **set)
{
(*set)=(Set *)malloc(sizeof(Set));
(*set)->value=0;
(*set)->next=NULL;
}
Boolean Set_Insert(Set **set, SetEntry value)
{
Set *new_set=(Set *)malloc(sizeof(Set));
Set *temp=*set;
if((*set)==NULL)
return(FALSE);
while(temp->next!=NULL)
{
if(temp->value==value)
return(FALSE);
else
temp=temp->next;
}
if(new_set==NULL)
return(FALSE);
else
{
new_set->value=value;
new_set->next=*set;
*set=new_set;
return(TRUE);
}
}
Boolean Set_Delete(Set **set, SetEntry value)
{
Set *temp=*set,*prev=*set;
if((*set)==NULL)
return(FALSE);
while(temp->next!=NULL)
{
if(temp->value==value)
{
if(temp==prev)
(*set)=(*set)->next;
else
prev->next=temp->next;
free(temp);
return(TRUE);
}
else
{
prev=temp;
temp=temp->next;
}
}
return(FALSE);
}
Boolean Set_Contains(const Set **set, SetEntry value)
{
const Set *temp=*set;
while(temp->next!=NULL)
{
if(temp->value==value)
return(TRUE);
else
temp=temp->next;
}
return(FALSE);
}
int Set_Size(const Set **set)
{
const Set *temp=*set;
int size=0;
while(temp->next!=NULL)
{
size++;
temp=temp->next;
}
return(size);
}
Boolean Set_Empty(const Set **set)
{
const Set *temp=*set;
int size=0;
while(temp->next!=NULL)
{
size++;
temp=temp->next;
}
return(size==0?TRUE:FALSE);
}
void Set_Clear(Set **set)
{
(*set)=NULL;
}
void Set_Union(const Set **set1, const Set **set2, Set **ret)
{
Set *new_set=(Set *)malloc(sizeof(Set)), *new_set2;
const Set *temp1=*set1, *temp2=*set2;
SetEntry temp_value;
Boolean entry=TRUE;
new_set->value=0;
new_set->next=NULL;
while(temp1->next!=NULL)
{
new_set2=(Set *)malloc(sizeof(Set));
new_set2->value=temp1->value;
new_set2->next=new_set;
new_set=new_set2;
temp1=temp1->next;
}
while(temp2->next!=NULL)
{
temp_value=temp2->value;
temp1=*set1;
entry=TRUE;
while(temp1->next!=NULL)
{
if(temp1->value==temp_value)
{
entry=FALSE;
break;
}
else
temp1=temp1->next;
}
if(entry==TRUE)
{
new_set2=(Set *)malloc(sizeof(Set));
new_set2->value=temp2->value;
new_set2->next=new_set;
new_set=new_set2;
}
temp2=temp2->next;
}
(*ret)=new_set;
}
void Set_Intersection(const Set **set1, const Set **set2, Set **ret)
{
Set *new_set=(Set *)malloc(sizeof(Set)), *new_set2;
const Set *temp1=*set1, *temp2=*set2;
SetEntry temp_value;
Boolean entry=FALSE;
new_set->value=0;
new_set->next=NULL;
while(temp1->next!=NULL)
{
temp_value=temp1->value;
temp2=*set2;
entry=FALSE;
while(temp2->next!=NULL)
{
if(temp2->value==temp_value)
{
entry=TRUE;
break;
}
else
temp2=temp2->next;
}
if(entry==TRUE)
{
new_set2=(Set *)malloc(sizeof(Set));
new_set2->value=temp_value;
new_set2->next=new_set;
new_set=new_set2;
}
temp1=temp1->next;
}
(*ret)=new_set;
}
double Set_Similarity(const Set **set1, const Set **set2)
{
Set *res1,*res2;
int size1,size2;
Set_Union(&(*set1),&(*set2),&res1);
size1=Set_Size(&res1);
Set_Intersection(&(*set1),&(*set2),&res2);
size2=Set_Size(&res2);
return((double)size2/(double)size1);
}
void Set_Print(const Set **set)
{
const Set *temp=*set;
printf("{");
while(temp->next!=NULL)
{
printf("%d,",temp->value);
temp=temp->next;
}
printf("}");
}
int main()
{
Set *set1=NULL, *set2=NULL, *ret=NULL;
SetEntry value;
Boolean result;
int choice, set_choice;
double res;
do
{
clrscr();
printf("\n\t----------MENU----------");
printf("\n\n\t1. Create Sets");
printf("\n\t2. Insert into Set");
printf("\n\t3. Delete from Set");
printf("\n\t4. Set Contains");
printf("\n\t5. Set Size");
printf("\n\t6. Set Empty");
printf("\n\t7. Clear Sets");
printf("\n\t8. Union of Sets");
printf("\n\t9. Intersection of Sets");
printf("\n\t10. Similarity of Sets");
printf("\n\t11. Print Sets");
printf("\n\t12. Exit");
printf("\n\n\t\tEnter your choice (1-12): ");
scanf("%d", &choice);
switch(choice)
{
case 1:
Set_Create(&set1);
printf("\n\n\t\tSet A created successfully.");
Set_Create(&set2);
printf("\n\n\t\tSet B created successfully.");
printf("\n\n\n\t\tPress any key to continue");
getch();
break;
case 2:
printf("\n\n\t\tEnter the value to be entered: ");
scanf("%d",&value);
printf("\t\tEnter the set in which to be entered (1/2): ");
scanf("%d",&set_choice);
if(set_choice!=1&&set_choice!=2)
printf("\n\n\t\tInvalid set selected for insertion.");
else if(set_choice==1)
result=Set_Insert(&set1,value);
else
result=Set_Insert(&set2,value);
if(result==FALSE)
printf("\n\n\t\tCan't insert the value into set.");
else
printf("\n\n\t\tValue Inserted Successfully.");
printf("\n\n\t\tPress any key to continue");
getch();
break;
case 3:
printf("\n\n\t\tEnter the value to be deleted: ");
scanf("%d",&value);
printf("\t\tEnter the set from which to be deleted (1/2): ");
scanf("%d",&set_choice);
if(set_choice!=1&&set_choice!=2)
printf("\n\n\t\tInvalid set selected for deletion.");
else if(set_choice==1)
result=Set_Delete(&set1,value);
else
result=Set_Delete(&set2,value);
if(result==FALSE)
printf("\n\n\t\tCan't delete the value from set.");
else
printf("\n\n\t\tValue Deleted Successfully.");
printf("\n\n\t\tPress any key to continue");
getch();
break;
case 4:
printf("\n\n\t\tEnter the value to be searched: ");
scanf("%d",&value);
printf("\t\tEnter the set in which to be searched (1/2): ");
scanf("%d",&set_choice);
if(set_choice!=1&&set_choice!=2)
printf("\n\n\t\tInvalid set selected for searching.");
else if(set_choice==1)
result=Set_Contains(&set1,value);
else
result=Set_Contains(&set2,value);
if(result==FALSE)
printf("\n\n\t\tValue is not present in the set.");
else
printf("\n\n\t\tValue is present in the set.");
printf("\n\n\t\tPress any key to continue");
getch();
break;
case 5:
printf("\n\n\t\tSize of Set 1 is: %d", Set_Size(&set1));
printf("\n\n\t\tSize of Set 2 is : %d", Set_Size(&set2));
printf("\n\n\n\t\tPress any key to continue.");
getch();
break;
case 6:
result=Set_Empty(&set1);
if(result==TRUE)
printf("\n\n\t\tSet 1 is empty.");
else
printf("\n\n\t\tSet 1 is not empty.");
result=Set_Empty(&set2);
if(result==TRUE)
printf("\n\n\t\tSet 2 is empty.");
else
printf("\n\n\t\tSet 2 is not empty.");
printf("\n\n\n\t\tPress any key to continue.");
getch();
break;
case 7:
Set_Clear(&set1);
printf("\n\n\t\tSet A cleared successfully.");
Set_Clear(&set2);
printf("\n\n\t\tSet B cleared successfully.");
printf("\n\n\n\t\tPress any key to continue");
getch();
break;
case 8:
Set_Union(&set1,&set2,&ret);
printf("\n\n\t\tUnion of two sets: ");
Set_Print(&ret);
printf("\n\n\n\t\tPress any key to continue.");
getch();
break;
case 9:
Set_Intersection(&set1,&set2,&ret);
printf("\n\n\t\tIntersection of two sets: ");
Set_Print(&ret);
printf("\n\n\n\t\tPress any key to continue.");
getch();
break;
case 10:
res=Set_Similarity(&set1,&set2);
printf("\n\n\t\tSet Similarity: %f",res);
printf("\n\n\n\t\tPress any key to continue.");
getch();
break;
case 11:
printf("\n\n\t\tSet 1: ");
Set_Print(&set1);
printf("\n\n\t\tSet 2: ");
Set_Print(&set2);
getch();
break;
case 12:
break;
default:
printf("\n\n\t\tInvalid choice. Try Again!!!");
printf("\n\n\t\tPress any key to continue.");
getch();
break;
}
}while(choice!=12);
return 0;
}