Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
#include <stdbool.h>
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
//function deeclartion
void push (struct node** head_ref, int new_data);
int ispresent(struct node* head, int data);
//function to get union of two linked list head1 and head2
struct node* getunion(struct node* head1,struct node* head2)
{
struct Node* result = NULL;
struct node *t1 = head1, *t2=head2;
//insert all element of list2 which are not present in result list
while (t2 !=NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
//insert those element of list2 which are not present in result list
while (t2 !=NULL)
{
if(!ispresnt(result,t2->data));
{
push(&result,t2->data);
}
t2 = t2->next;
}
return result;
}
// function to get intersection of two linked lists head1 and head2
struct Node* getIntersection(struct Node * head1, struct Node* head2)
{
struct node* result = NULL;
struct node* t1 = head1;
//traverse list1 and search each element of it in list2. if the element is present in list 2, then inser!
while (t1 != NULL)
{
if (ispresent(head2, t1->data))
{
push(&result,t1->data);
}
t1 =t1->next;
}
return result;
}
// a utility function to insert a node at the begining of a linked list
void push(struct Node** new_ref, int new_data)
{
//allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
//put in the data
new_node->data=new_data;
//link
}
void printlist(struct Node* Node)
{
while (Node ! = NULL)
{
printf("%d",Node->data);
Node = Node->next;
}
}
//function that returns true if true data is present in linked list else return false
int ispresent(struct node* head,int data)
{
struct node* t =head;
while (t !=NULL)
{
if (t->data = data)
{
return 1;
}
t =t->next;
}
return 0;
}
//driver program to test above function
int main()
{
int n,m,temp;
printf("enter the value of number of vaue in link list(both):-");
fflush(stdout);
scantf("%d %d",&n,&m);
// starth stucture pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link list1
for (int i=0;i<n;i++)
{
printf("enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head1,temp);
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head2,temp);
}
//call the function of intersection
intersecn = getintersection(head1,head2);
//call the function of union
unin = getunion(head1,head2);
printf("n first list is ");
//function calling with structure pointer as argument
orintlist(head1);
printf("n second list is ");
printflist(head2);
printf("n intersection list is ");
printlist(intersecn);
printf("n union list is ");
printlist(unin);
return 0;
}