Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Embedded Batch 2(offline)
//NAME:LALITH KUMAR M
#include
#include
// Link list node
struct node
{
int data;
struct node *next;//self referential node
};
//Define the function
//A utility function to insert a node at the beginning of a linked list
void push(struct node**head_ref,int key);
int isPresent(struct node *head,int data);
//Function to get union of two linked lists head1 and head2
struct node *getunion(struct node *head1,struct node *head2)
{
struct node *result=NULL;
struct node *x1=head1,*x2=head2;
// Insert all elements of
//first_set_items to the result list
while (x1!=NULL)
{
push(&result,x1->data);
x1=x1->next;
}
// Insert those elements of second_set_items
// which are not present in result second_set_items
while (x2!=NULL)
{
if(!isPresent(result,x2->data))
{
push(&result,x2->data);
}
x2=x2->next;
}
return result;
}
struct node* getintersec(struct node* head1,
struct node* head2)
{
struct node* result = NULL;
struct node* t1 = head1;
// Traverse second_set_items and search each element of it in
// second_set_items If the element is present in list 2, then
// insert the element to result
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
push(&result, t1->data);
t1 = t1->next;
}
return result;
}
void push(struct node**head_ref,int key)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
//put in the data
newnode->data=key;
//link the old list off the new node
newnode->next=(*head_ref);
//move the head to point to the new node
(*head_ref)=newnode;
}
//A utility function to print a linked list
void display(struct node*head)
{
struct node *k;
k=head;
while(k!=NULL)
{
printf(" %d ",k->data);
k=k->next;
}
}
//A utility function that returns true 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;
}
int main()
{
//Start with the empty list
struct node *head1=NULL;
struct node *head2=NULL;
struct node *reset_union=NULL;
struct node *reset_intersection=NULL;
int first_set_nodes = 0, first_set_items = 0;
int second_set_nodes = 0, second_set_items = 0;
printf("\nenter the value of number of value in link list one= ");
scanf("%d", &first_set_nodes);
printf("Enter All %d elements in link list one= ", first_set_nodes);
for(int i = 0; i < first_set_nodes; i++ )
{
scanf("%d",&first_set_items);
push(&head1,first_set_items);
}
printf("\n linklist one =");
display(head1);
printf("\nenter the value of number of value in link list two= ");
scanf("%d", &second_set_nodes);
printf("\nEnter All %d elements in link list two= ", second_set_nodes);
for(int i = 0; i < second_set_nodes; i++ )
{
scanf("%d",&second_set_items);
push(&head2,second_set_items);
}
printf("\n linklist two = ");
display(head2);
printf("\n-------------------------------------------\n");
reset_union = getunion(head1, head2);
printf("\nUnion of data of two list = ");
display(reset_union);
printf("\n");
reset_intersection = getintersec(head1, head2);
printf("Intersection of data of two list = ");
display(reset_intersection);
return 0;
}