Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
1.SOLLUTION:-
CODE:-
#include <stdbool.h> //preprocessor header and libraray for boolean expression
#include <stdio.h> //preprocessor and header library for standard input
#include <stdlib.h> //preprocessor and header library for memory allocation, process control, conversions and others.
// Link list node
struct Node //initilizes the structure data type
{
int data; //declared the integer data type
struct Node* next; //It is a pointer to an element of a structure node and stores the address of next variable
};
//function declaration
void push(struct Node** head_ref, int new_data); // the head of a list and an int, inserts a new node on the front of the list.
int isPresent(struct Node* head, int data); //initlizes the data in pointer of user 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; //creating a linked list
struct Node *t1 = head1, *t2 = head2; // Traverse both lists and store the element in the resu1tant list.
// Insert all elements of list1 to the result list
while (t1 != NULL) //initilizes he while loop if list1 is not equal to NULL
{
push(&result, t1->data); //push the data into list 1 and store in result
t1 = t1->next; // data is returned to store and next user input data in list 1
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL) //initilizes the while loop list2 not present in result
{
if (!isPresent(result, t2->data)) //initilizes the if loop list2 not present in result
{
push(&result, t2->data); //push the data inti list2
}
t2 = t2->next; // data is returned to store and next user input data in list2
}
return result; //main function returning to the 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; //if the poniter location of node of result is null
struct Node* t1 = head1; //store data in pointer of head of list 1 data
// Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result
while (t1 != NULL) //initilizes the while loop list is not equal to null
{
if (isPresent(head2, t1->data)) //if loop data transverse to list 1 into head 2 location
{
push(&result, t1->data); //push the result of data into list1 into result
}
t1 = t1->next; // data is returned to store and next user input data in list 1
}
return result; //main function returning to the result
}
// A utility function to insert a node at the beginning of a linked list
void push(struct Node** head_ref, int new_data)
{
// allocate node and its size of 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 = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to print a linked list
void printList(struct Node* node)
{
while (node != NULL) //initilizes the node data not equal to NULL
{
printf("%d ", node->data); //print the node data
node = node->next; //store the next input data from user
}
}
// 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; //node of pointer location to head of data
while (t != NULL) //initilizes the while loop transeverse list not equal to null
{
if (t->data == data) //initilizes data transper into transverse to data
{
return 1; //main function successfully executed by aruguments of function by returning 1
}
t = t->next; //transverse list data stores next input from user
}
return 0; //main function successfully executed by returning by 0
}
// Driver program to test above function
int main() //initilizes the main function
{
int n,m,temp; //declared the variables n,m and temp
printf("enter the value of number of value in link list(both):-"); //print enter the values of number in link list(both)
fflush(stdout); //clear (or flush) the output buffer and move the buffered data to console
scanf("%d %d", &n, &m); //read data from user of n and m varaibles
// Starth structure pointer with the empty list
struct Node* head1 = NULL; //structure pointer of head1 with empty list
struct Node* head2 = NULL; //structure pointer of head2 with empty list
struct Node* intersecn = NULL; //structure pointer of node of intersecton with empty list
struct Node* unin = NULL; NULL; //structure pointer of node of union with empty list for loop for link list1
for(int i=0;i<n;i++)
{
printf("Enter the value in link list1 %d:-",i); //print enter values in list 1
fflush(stdout); //clear (or flush) the output buffer and move the buffered data to console
scanf("%d", &temp); //read the temporary data
push(&head1, temp); //push the data`from head 1 into temporary storage
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("Enter the value in link list2 %d:-",i); //print the enter values in list 1
fflush(stdout); //clear (or flush) the output buffer and move the buffered data to console
scanf("%d", &temp); //read the temporary data
push(&head2, temp); //push the data`from head 2 into temporary storage
}
//call the function of intersection
intersecn = getIntersection(head1, head2);
//call the function of union
unin = getUnion(head1, head2);
printf("\n First list is "); //prin the first list
//function calling with structure pointer as argumen
printList(head1); //print list of hea1
printf("\n Second list is "); //print the second list of values
printList(head2); //print the head 2 list of values
printf("\n Intersection list is "); //print the intersection list of values
printList(intersecn); //print the intersection list of both data valeus
printf("\n Union list is "); //print the union list of both list values
printList(unin); //print the union of both list values of data
return 0; //main function successsfully executed by returning 0
}
OUTPUT:-
Enter the value of number of value in link list(both):-3 4
Enter the value in link list1 0:-2
Enter the value in link list1 1:-3
Enter the value in link list1 2:-4
Enter the value in link list2 0:-4
Enter the value in link list2 1:-5
Enter the value in link list2 2:-6
Enter the value in link list2 3:-7
First list is 4 3 2
Second list is 7 6 5 4
Intersection list is 4
Union list is 5 6 7 2 3 4
Screenshot:-
