Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
#include
struct node
{
int data;
struct node *next;
};
void push(struct node** head_ref,int new_data);
bool ispresent(struct node* head,int data);
struct node* getunion(
struct node* head1,
struct node* head2)
{
struct node* result = NULL;
struct node *t1 = head1, *t2 = head2;
while(t1!=NULL){
push(&result,t1->data);
t1=t1->next;
}
while(t2 != NULL){
push(&result,t2->data);
t2=t2->next;
}
return result;
}
struct node* getintersection(struct node* head1,
struct node* head2)
{
struct node* result =NULL;
struct node* t1=head1;
while(t1!=NULL){
push(&result,t1->data);
t1 = t1->next;
}
return result;
}
void push(struct node** head_ref,int new_data)
{
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=new_data;
new_node->next = (*head_ref);
(*head_ref)=new_node;
}
void printlist(struct node* node)
{
while(node!=NULL){
printf("%d",node->data);
node=node->next;
}
}
bool isPresent(struct node* head,int data)
{
struct node* t = head;
while(t!=NULL){
if(t->data==data)
return 1;
t=t->next;
}
return 0;
}
int main()
{
int a,b,c,d,w,x,y,z;
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* intersection = NULL;
struct node* Union = NULL;
printf("enter the 4 elements for the 1st list:");
scanf("n%d",&a);
scanf("n%d",&b);
scanf("n%d",&c);
scanf("n%d",&d);
fflush(stdout);
printf("enter the 4 elements for the 2nd list:");
scanf("n%d",&w);
scanf("n%d",&x);
scanf("n%d",&y);
scanf("n%d",&z);
fflush(stdout);
push(&head1,a);
push(&head1,b);
push(&head1,c);
push(&head1,d);
push(&head2,w);
push(&head2,x);
push(&head2,y);
push(&head2,z);
intersection = getintersection(head1,head2);
Union = getunion(head1,head2);
printf("n first list is n");
printlist(head1);
printf("nsecond list is n");
printlist(head2);
printf("n intersection list is n");
printlist(intersection);
printf("n union list is n");
printlist(Union);
return 0;
}