Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
struct Node
{
int DATA;
struct Node* NEXT;
// pointer to the next node in the linked list and stablishes the connection between nodes
};
//add a new node to the beginning of a linked list
void push(struct Node** head_ref, int new_DATA);
//struct Node** head_ref --> first parameter
// It's a pointer to a pointer to a struct Node
// int new_DATA --> second paramter
/*using a double pointer, changes made to head_ref within the function will reflect outside the function, allowing the function to modify the head of the linked list.*/
// function appears to be designed to check if a particular data value
bool isPresent(struct Node* head, int DATA);
//isPresent --> check whether the specific value is present
/*struct Node* head --> parameter that tells the function where to start looking for the data in the LL
--> pointer to the first node of the linked list.*/
//int DATA --> another parameter that specifies data value you want to check for in LL
struct Node* getUnion(struct Node* head1, struct Node* head2)
{
struct Node* result = '\0';
//initializes a pointer result to NULL
struct Node *x1 = head1, *x2 = head2;
//head1 and head2, which are pointers to the heads of two LL that you want to find the union of it
// Traverse the first input list (head1)
while (x1 != '\0')
{
push(&result, x1->DATA);
// Add the current node's data to the result list
x1 = x1->NEXT;
// Move to the next node in the first input list
}
while (x2 != '\0') //if condition true means it will execute
{
if (!isPresent(result, x2->DATA))
//checks if the value stored in x2
push(&result, x2->DATA);
//push calls the function and add the value x2 to the result
x2 = x2->NEXT; //x2 pointer to point to the next node
}
return result;
}
//represents the result of the intersection operation in LL
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = '\0';
//initializes a pointer called result to '\0'
struct Node* x1 = head1;
//ptr variable x1 --> initialized to the head of the first input LL, head1. This pointer will be used to traverse the head1 list.
while (x1 != '\0')
//loop iterates over the elements of head1 until it reaches the end of the list
{
if (isPresent(head2, x1->DATA))
//This line checks if the value x1->DATA from head1 is also present in the head2 list
push(&result, x1->DATA);
//that elements common to both head1 and head2 are added to the result list.
x1 = x1->NEXT;
//used to move x1 to the next node in the head1 list
}
return result;
}
void push(struct Node** head_ref, int new_DATA)
{
// allocate the separate memory for the list
struct Node* new_node= (struct Node*)malloc(sizeof(struct Node));
//above line allocates memory for a new node on the heap using the malloc function
new_node->DATA = new_DATA;
// line sets the DATA field of the newly created node to the value of new_DATA
//assigns the data you want to store in the new node.
new_node->NEXT = (*head_ref);
//NEXT --> field of the new node to point to the current head of the linked list.
(*head_ref) = new_node;
//to point to the newly created node
}
//this function takes a pointer to the head of the linked list as an argument
void printList(struct Node* node)
{
while (node != '\0') // if the node is not equal to zero
{
printf("%d ", node->DATA);
// print the data in the new node
node = node->NEXT;
}
}
bool isPresent(struct Node* head, int DATA)
{
struct Node* x = head;
// Create a temporary pointer 'x' and initialize it with the head of the linked list
while (x != NULL)
{
if (x->DATA == DATA)
// Check if the 'DATA' in the current node pointed to by 'x' is equal to the target 'DATA'
// If this condition satisfies ,it will return 1
return 1;
x = x->NEXT;
}
return 0;
}
int main()
{
// always the list is starting with NULL
struct Node* head1 = '\0';
struct Node* head2 = '\0';
struct Node* intersecn = '\0';
struct Node* unin = '\0';
// creating the linked list one using user input
push(&head1, 8769);
push(&head1, 3467);
push(&head1, 3987);
push(&head1, 9246);
push(&head1, 4812);
// creating the linked list two using user input
push(&head2, 3987);
push(&head2, 9856);
push(&head2, 2552);
push(&head2, 1086);
push(&head2, 9246);
intersecn = getIntersection(head1, head2);
unin = getUnion(head1, head2);
// below this all are printing statement
printf("\nTHE ELEMENT IN THE FIRST LIST IS :- \n");
printList(head1);
printf("\nTHE ELEMENT IN THE SECOND LIST IS:- \n");
printList(head2);
printf("\nINTERSECTION OF THE FIRST AND SECOND LIST IS:- \n");
printList(intersecn);
printf("\nUNION OF THE FIRST AND SECOND LIST IS:- \n");
printList(unin);
return 0;
}