Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
-----------------------------------------------------------LIVE BATCH---------------------------------------------
AIM:-Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
- CODE:-
Main.C:-
#include"func.h"
int main()
{
//head1=head2=0;
int n1,n2,x,i;
P("enter value of node in linked list 1:-");
fflush(stdout);
S("%d",&n1);
P("enter value of node:");
for(i=0;i<n1;i++)
{
fflush(stdout);
S("%d",&x);
insert(&head1,x);
}
P("\nenter value of node in linked list 2:-");
fflush(stdout);
S("%d",&n2);
P("enter value of node:");
for(i=0;i<n2;i++)
{
fflush(stdout);
S("%d",&x);
insert(&head2,x);
}
P("\nlinked list 1:-");
display(head1);
P("\nlinked list 2:-");
display(head2);
do_union(head1,head2);
P("\n\nunion result of linked list 1 & 2:-");
display(u_result);
do_intersection(head1,head2);
P("\n\nintersection result of linked list 1 & 2:- ");
display(i_result);
return 0;
}
func.c:-
#include"func.h"
struct node *insert(struct node **t1,int n)
{
struct node *new;
new=(struct node *)malloc(sizeof(struct node));
new->value=n;
new->next=*t1;
*t1=new;
return 0;
}
struct node *do_union(struct node *l1,struct node *l2)
{
u_result=NULL;
struct node *ur=l1;
while(ur!=NULL)
{
insert(&u_result,ur->value);
ur=ur->next;
}
while(l2!=NULL)
{
if(!search(l1,l2->value))
{
insert(&u_result,l2->value);
}
l2=l2->next;
}
return u_result;
}
struct node *do_intersection(struct node *l1,struct node *l2)
{
i_result=NULL;
while(l1!=NULL)
{
if(search(l2,l1->value))
{
insert(&i_result,l1->value);
}
l1=l1->next;
}
return u_result;
}
struct node *search(struct node *pp,int n)
{
while(pp!=0)
{
if(pp->value==n)
{
return (1);
}
pp=pp->next;
}
return 0;
}
struct node *display(struct node *p)
{
while(p!=0)
{
P("%d ",p->value);
p=p->next;
}
return 0;
}
func.h:-
#ifndef FUNC_H_
#define FUNC_H_
#include<stdio.h>
#include<stdlib.h>
#define P printf
#define S scanf
struct node
{
int value;
struct node *next;
};
struct node *head1,*head2,*u_result,*i_result;
struct node *display(struct node *);
struct node *insert(struct node **,int );
struct node *do_union(struct node *,struct node *);
struct node *search(struct node *,int n);
struct node *do_intersection(struct node *,struct node *);
#endif /* FUNC_H_ */
- RESULT:-
