Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};//link list
void push(struct node** head,int new);
void printlist(struct node* node);//function prototype
int next(struct node* head,int data);
/*function to find the union of two nodes here we use condition to expand
* the node.
*/
struct node* getunion(struct node* head1,struct node* head2)
{
struct node* result =NULL;
struct node*h1= head1,*h2= head2;
while(h1!=NULL)
{
push(&result,h1->data);
h1=h1->next;
}
while(h2!=NULL)
{
if (!next(result,h2->data))
{
push(&result,h2->data);
}
h2=h2->next;
}
return result;
}
/*function to find the intersection of two node
*
*/
struct node* getintersection(struct node* head1,struct node *head2)
{
struct node* result=NULL;
struct node*t1=head1;
while(t1!=NULL)
{
if(next(head2,t1->data))
{
push(&result,t1->data);
}
t1=t1->next;
}
return result;
}
void push(struct node** head,int node)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=node;
new_node->next=(*head);
(*head)= new_node;
}
void display(struct node* node)
{
while(node!=NULL)
{
printf("%d",node->data);
node=node->next;
}
}
int next(struct node*head,int data)
{
struct node*n=head;
while(n!=NULL)
{
if (n->data==data)
{
return 1;
}
n=n->next;
}
return 0;
}
int main()
{
int a,b,temp;
printf("Enter the value of number of value in link list(both):");
fflush(stdout);
scanf("%d %d",&a,&b);
struct node*head1=NULL;
struct node*head2=NULL;
struct node*intersecn=NULL;
struct node*unin=NULL;
for(int i=0;i<a;i++)
{
printf("Enter the value in the linked list1 %d:",i);
fflush(stdout);
scanf("%d",&temp);
push(&head1,temp);
}
for(int i=0;i<b;i++)
{
printf("Enter the value in the linked list2 %d:",i);
fflush(stdout);
scanf("%d",&temp);
push(&head2,temp);
}
intersecn=getintersection(head1,head2);
unin=getunion(head1,head2);
printf("\nFirst list is ");
display(head1);
printf("\nSecond list is ");
display(head2);
printf("\n Intersection list is");
display(intersecn);
printf("\n Union list is ");
display(unin);
return 0;
}