Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
Ans-Implementation of interface for mathematical Set Theory using C programming.
1-Union
2-Intersection
Program->
#include <stdio.h> //header file
#include<stdlib.h>
struct node {
int data;
struct node *next; //self referential structure or linked list structure
}*num1,*num2,*tmp,*ptr; //global variable
int main()
{
printf("first group element should be less than 7\n element should not repeat more than one in any group");
int p,q;
struct node *fn;
printf(" \nnumber of elements=");
scanf("%d",&q);
printf("type 1 element=");
num1=malloc(sizeof(struct node)); //memory allocation for num1
scanf("%d",&p);
num1->data=p;
num1->next=NULL;
ptr=num1;
for(int i=2;i<=q;i++)
{
fn=malloc(sizeof(struct node)); //memory allocation for fn
printf("type %d element=",i);
scanf("%d",&p); //reading values from console
fn->data=p;
fn->next=NULL;
ptr->next=fn;
ptr=ptr->next;
}
ptr=num1;
printf("first group elements={");
while(ptr!=NULL)
{
printf("%d,",ptr->data); //first group element writing on console
ptr=ptr->next;
}printf("}");
printf("\nnumber of elements");
{
int p,y;
struct node *fn;
scanf("%d",&y);
printf("\ntype your second group element=");
num2=malloc(sizeof(struct node));
scanf("%d",&p);
num2->data=p;
num2->next=NULL;
tmp=num2;
for(int i=2;i<=y;i++)
{
fn=malloc(sizeof(struct node)); //second group element reading from console
printf("type %d element=",i);
scanf("%d",&p);
fn->data=p;
fn->next=NULL;
tmp->next=fn;
tmp=tmp->next;
}
}
{
tmp=num2;
printf("second group elements={");
while(tmp!=NULL) //second group element writing on console
{
printf("%d,",tmp->data);
tmp=tmp->next;
}
printf("}");
}
{
ptr=num1;
tmp=num2;
}
printf("\nunion1={");
while(ptr!=NULL)
{
//condition for union just I am printing first group elements
printf("%d,",ptr->data);
ptr=ptr->next;
}
ptr=num1;
while(tmp!=0) //condition that we are adding second group elements which is not present in first group
{
if(tmp->data!=ptr->data && tmp->data!=ptr->next->data && tmp->data!=ptr->next->next->data && tmp->data!=ptr->next->next->next->data && tmp->data!=ptr->next->next->next->next->data&& tmp->data!=ptr->next->next->next->next->next->data && tmp->data!=ptr->next->next->next->next->next->next->data)
{
printf(",%d",tmp->data);
//checking element by element
}
tmp=tmp->next;
}
printf("}\n");
printf("intersection={"); //condition for intersection
ptr=num1;
tmp=num2;
while(ptr!=NULL)
{ tmp=num2;
while(tmp!=NULL)
{
if(tmp->data==ptr->data) //if elements are present in both the group then we should write on console
{
printf("%d,",tmp->data);
}
tmp=tmp->next;
}
ptr=ptr->next;
}
printf("}");
}
Output-
