Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stidio.h>
#include<stdio.h>
#include <stdlib.h>
// link list node
struct node {
int data ;
struct node* next ;
};
//function declaration
void push ( struct node** hend_ref ,int new data);
int ispresent (struct node*head ,int data);
//function to get union of two linked lists hend1 and hend2 struct node* getunion(struct node*hend1 struct node 2)
{
struct node* result = null;
struct node *t1 =hend 1, *t2 = hend2;
// insert all elements of list1 to the result list while (t1 != null)
{
push (&ruselts, t1->data );
t1 = t1->next ;
}
// insert those elements of list2 which are not present in ruselt list while (t2 !=null)
{
if (! ispresent(ruselt,t2->data));
{
push (&result, t2->data);
}
t2 = t2->next;
}
return results;
}
//function to get intersection of two linked lists hend1 and hend2 struct node* getintersecction(struct node*hend1 struct nide*hend2)
{
struct node* result = null;
struct node* t1 = hend1;
//traverse list1 and search each element of it in list2. if the element is present in list 2 then insert the element while (t1 !=null)
{
if (ispresent(hend2,t1->data))
{
push(&result,t1->data);
}
t1 = t1 ->next ;
}
retuer result;
}
//a utility function to insert a node at the beginning of a linked list void push ( struct node** hend_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 the old list off the new node
new_node ->next=(*hend_ref);
// move the hend to point to the new node
(*hend_ref) = new_node;
}
// function to print a licked list void printlist ( struct node* node)
void printlist(struct node*node)
{
while (node ! =null)
{
printf("%d", node->data);
node = node->next;
}
}
// function that returns ture if 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 if value in link list(both):-");
fflush(stdout);
scanf("%d %d, &n, &m);
//starth structure pointer with the empty list
struct node* head1 = null;
struct node* hend2 = 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(&hend1, 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(&hend2, temp);
}
//call the function in intersection
intersecn = getintersection(hend1, hend2);
//call the function of union
unin= getunion(hend1, hend2);
printf("\n first list is");
//function calling with structure pointer as argument
printlist(hend1);
printf("\n second list is ");
printlist(hend2);
printf("\n intersection list is ");
printf(intersecn);
printf('\n union list is");
printlist(union);
return 0;
}