Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdio.h>
#include<stdlib.h>
//defined structure node here
struct Node {
int data;
struct Node* next;
};
//included all function here because all function can recognize every function
int present(struct Node *head, int data);
struct Node* Union_list(struct Node* head1, struct Node* head2);
void insert(struct Node** head, int new_data);
void printList(struct Node* node);
// this function will check if data is present in list or not
int present(struct Node* head, int data)
{
struct Node* t = head;
while (t!=NULL)
{
if(t->data == data)
{
return 1;
}
t=t->next;
}
return 0;
}
// function is to get the union data;
struct Node* Union_list(struct Node* head1, struct Node* head2)
{
struct Node* list=NULL;
struct Node* t1= head1;
struct Node* t2= head2;
while(t1!=NULL)//it will check all data in list, insert in duplicate list called list
{
insert(&list, t1->data);
t1=t1->next;
}
while(t2!=NULL) // it will check for not listed nodes in above, check in second node;
{
if(!present(list, t2->data))
{
insert(&list, t2->data);
}
t2=t2->next;
}
return list;// it will return the whole list data to the requested in main.
}
struct Node* intersection_list(struct Node* head1, struct Node* head2) // function takes two linked list, gives output as intersection
{
struct Node* list2=NULL;
struct Node* t3=head1;
while(t3 !=NULL) // check for all linked list in data given, checks with every data in list of second list
{
if(present(head2, t3->data))// it will only insert if present in both lists
{
insert(&list2, t3->data);
}
t3=t3->next;
}
return list2; // return to the requested in main
}
void insert(struct Node** head, int new_data) // insert data into the list
{
struct Node *new_node= (struct Node*) malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next = (*head);
(*head)= new_node;
}
void printList(struct Node* node) // prints the all data in linked list
{
while (node != NULL)
{
printf(" %d,", node->data);
node = node->next;
}
}
int main()
{
//declared ints used program
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d", &m, &n); // it will ask and take inputs from user
// Starth structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersection_set = NULL;
struct Node* union_set = NULL;
for(int i=0; i<m; i++) // it is to take inputs from user for all data to set 1
{
int new_data;
printf("enter the value in lsit 1 for %d:", i);
scanf("%d", &new_data);
insert(&head1, new_data);
}
printf("set1 ={");
printList(head1);
printf("}"); // it will output the data in set1
for (int i=0; i<n; i++) //it is to take inputs from user for all data to set 2
{
int new_data;
printf("enter the values for lsit 2 %d:");
scanf("%d", &new_data);
insert (&head2, new_data);
}
printf("set2 ={");
printList(head2);
printf("}");// prints out the whole data in list2
intersection_set = intersection_list(head1, head2); // it will call the intersection function, insert in intersection_set.
printf("intersection= {");
printList(intersection_set);
printf("}"); // it will prints out the intersection data.
union_set = Union_list(head1, head2);
printf("union= {");
printList(union_set);
printf("}"); // it will prints out the union set data
}