Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
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> //header files
#include <stdlib.h>
typedef struct node
//creation of structure named node with two members of integer data and a pointer to another Node structure named "next",
{
int data;
struct node* next;
} Node;
Node* create_node(int data);
//creation of function named createnode with given integer data retuns pointer to the newley created node
Node* insert_node(Node* head, int data);
//creation of function named insertnode inserts a new node with the given data value at the beginning of the linked list pointed to by the head pointer,
Node* set_union(Node* set1, Node* set2);
//inthis fuction named set_union will take list of two sets of elements and returns a new list with union two lists
Node* set_intersection(Node* set1, Node* set2);
//inthis fuction named set_intersection will take list of two sets of elements and returns a new list with elemnts that are common elements in both sets
int main()
{
Node *set1 = NULL, *set2 = NULL, *union_set = NULL, *intersection_set = NULL;
//under main function we are create nodes set1 pointer , set2 pointer ,union set,intersection set and we are setting the values of them as NULL(0), pointers used to store the heads of linked lists that represent sets and their union and intersection.
int size1, size2, data; //three integers size1,size2 and data are used .data used to store the value entered by the user
//user to enter the size of set1 and store the size of set1 in size1 integer
printf("Enter the size of set 1: "); fflush(stdout);
scanf("%d", &size1);
//user to enter the elements of set1 and store the elements of set1 in size1
printf("Enter elements of set 1:\n");
fflush(stdout);
for (int i = 0; i < size1; i++)
//for loop used to check wether entered elements are inside the given size in set 1 or not
{
scanf("%d", &data);
set1 = insert_node(set1, data);
//data from user will be stored into the newlinked list set1 using insert_node function
}
printf("Enter the size of set 2: ");
//user to enter the size of set2 and store the size of set1 in size1 integer
fflush(stdout);
scanf("%d", &size2);
printf("Enter elements of set 2:\n");
//user to enter the elements of set1 and store the elements of set2
fflush(stdout);
//for loop used to check wether entered elements are inside the given size in set 2 or not
for (int i = 0; i < size2; i++)
{
scanf("%d", &data);
//data from user will be stored into the newlinked list set 2 using insert_node function
set2 = insert_node(set2, data);
}
union_set = set_union(set1, set2);
//set_union function perform linked list set1&set2 with union of two list will be stored in union_set
intersection_set = set_intersection(set1, set2);
//set_intersection function perform linked list of set1&set2 intersection of two list will be stored in intersection_set
printf("Union of sets:\n"); //printing the union of new sets
while (union_set != NULL) //while loop to check the list untill it is not zero
{
printf("%d ", union_set->data);
//printing the value of each node pointed by the union_set pointer into the list
union_set = union_set->next;
//union_set pointer is then updated to point to the next node in the list using the next pointer of the current node, which is accessed using the -> operator.
}
printf("\n");
printf("Intersection of sets:\n"); //printing the intersection of new sets
while (intersection_set != NULL) //while loop to check the list untill it is not zero
{
printf("%d ", intersection_set->data);
// print the data value of the node pointed to by the intersection_set pointer to the console.
intersection_set = intersection_set->next;
//intersection_set pointer is then updated to point to the next node in the list using the next pointer of the current node, which is accessed using the -> operator.
}
printf("\n");
return 0;
}
Node* create_node(int data) //with userdata we create new node
{
Node* new_node = (Node*)malloc(sizeof(Node)); //using dynamic mem.allocator malloc() to allocate the size required to store a single node in the linked list this function returns a pointer to the allocated block of memory
new_node->data = data; //new node will be allocate the data field of newly allocated node
new_node->next = NULL; // set the next field of the new node to NULL, which means that it currently does not point to any other node in the linked list. assume it is the last node until next poinetd to it
return new_node; //returns a pointer to a newly allocated node
}
Node* insert_node(Node* head, int data) //this function insert_node have two arguments the pointer to the head of a linked list (Node* head) and an integer value to be inserted into the list
{
if (head == NULL) //if head list is empty it will create a new head list with specified data values and make a new head list
{
return create_node(data);
}
Node* current = head; //head list will be equal to the new pointer named current
while (current->next != NULL) //while head list is not equal to zero
{
current = current->next; //update the current list iterates throughout the list one node at a time
}
current->next = create_node(data);
//creates a new node with the provided data and sets the "next" pointer of the last node in the linked list to point to the newly created node.
return head;
}
Node* set_union(Node* set1, Node* set2) //set_union function perform union on set1 and set2 linked lists
{
Node *result = NULL, *current; //creating a pointer current and result will be intially null
while (set1 != NULL)
//while loop itreate through the linked list set1 untill it is not equal to zero
{
result = insert_node(result, set1->data);
//insert the data from the current node of "set1" into the "result" linked list.
set1 = set1->next;
//inserting the data from the current node of "set1" into "result", the pointer "set1" is updated to point to the next node in the "set1" linked list so that the loop can continue with the next element in "set1".
}
while (set2 != NULL)
//while loop itreate through the linked list set2 untill it is not equal to zero
{
current = result; //creation of current node and result is equal to the current
while (current != NULL && current->data != set2->data)
//this code searching for a node with specific data in a linked list. It uses a loop to iterate through the linked list and checks the data stored in each node until it finds a match or reaches the end of the list.
{
current = current->next;
// value of "current->next" is assigned to the pointer "current", which moves the pointer to the next node in the linked list.
}
if (current == NULL)
{
result = insert_node(result, set2->data);
// the pointer "current" has reached the end of the linked list. If it has, the data stored in the node pointed to by "set2" is inserted into the "result" linked list
}
set2 = set2->next; //next node in the linked list pointed to by "set2" to the pointer "set2".
}
return result;
}
Node* set_intersection(Node* set1, Node* set2)
//fuction performs the intersection on linked list of both set1 and set2
{
Node *result = NULL, *current; //result pointer is intialised at zero and current pointer is intialised to result pointer
while (set1 != NULL) //while loop check for the set1 values are zero or not
{
current = set2; //current is assumed to be equal to the set2 list
while (current != NULL && current->data != set1->data)
//this loop that iterates through the linked list pointed to by "current" until it finds a node with the same data as the node pointed to by "set1".
{
current = current->next; //this line chcek for element by element untill it reaches last node. this line pointed toward next node to the current list
}
if (current != NULL) //condition is used to ensure that "set1" can be inserted into "result" before "current" reaches the end of the linked list.
{
result = insert_node(result, set1->data); //"insert_node" function to insert the node pointed to by "set1" into the linked list pointed to by "result".
}
set1 = set1->next;
//updates the pointer "set1" to point to the next node in the linked list using "set1 = set1->next". This ensures that the loop will iterate through each node in the linked list pointed to by "set1".
}
return result; //returns the pointer to the head of the merged linked list pointed to by "result".
}
OUTPUT:
