Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/* project set theory.c
*
* Created on: Mar 4, 2023
* Author: Anbudan Balaji
*/
#include <stdio.h> //header file library
#include <stdlib.h>//header file library
struct node //structure of linked list node
{
int data;
struct node *next;
} *head_pointer1, *head_pointer2, *head_union, *head_intersection;
void insert_node(struct node **head, int num) //insert a new node in beginning of a singly linked list.
{
struct node* newnode = (struct node*) malloc(sizeof(struct node)); //memory allocation of new node
newnode->data = num; //assign number value to data of new node
newnode->next = *head; // Next pointer of new node will point to head node
*head = newnode; //head pointer to point the new node and assign address of new node
}
int compare(struct node *head, int num) // compare an element with data field of each node in Linked List starting from head pointer
{
while (head != NULL) //loop will continue untill end of linked list is reached
{
if (head->data == num) //check data field of current node pointed by head is equal to number
return 1; //return integer value 1 to indicating the value was found in linked list
head = head->next; //loop can continue until end of link list reached
}
return 0; // return integer value 0 to indicating the value was not found in linked list
}
struct node* get_union(struct node *head_pointer1, struct node *head_pointer2) //union of two given linked list
{
struct node *temp = head_pointer1; //temp pointer variable assign the value of head pointer1
while(temp != NULL) //while loop that traverses the linked list pointed by header pointer 1
{
insert_node(&head_union, temp->data); //insert each node data value in head union linked list
temp = temp->next; //temp is moved to next node
}
while(head_pointer2 != NULL) // while loop that traverse the linked list pointed by head pointer 2
{
if(!compare(head_pointer1, head_pointer2->data)) //checks if the data value of node pointed by head pointer 2 exists in head pointer 1
{
insert_node(&head_union, head_pointer2->data); //insert data into head union link list
}
head_pointer2 = head_pointer2->next; //head pointer 2 is moved to next node
}
return head_union; //return pointer to head union which contain the union of head pointer 1 and head pointer 2
}
struct node* get_intersection(struct node *head_pointer1, struct node *head_pointer2) //intersection of two given linked list
{
while(head_pointer1 != NULL) //while loop that traverse the linked list pointed by head pointer 1
{
if(compare(head_pointer2, head_pointer1->data)) //Search every element of head pointer 1 in head pointer 2, If found then add it to intersection List
{
insert_node(&head_intersection, head_pointer1->data); //insert new node link list pointed by head intersection using head pointer 1 data
}
head_pointer1 = head_pointer1->next; //head pointer 1 to point the next node in linked list
}
return head_intersection; //return pointer to head intersection which contain the intersection of head pointer 1 and head pointer 2
}
void printLinkedList(struct node *nodePtr) //Prints a linked list from head node to tail node
{
while (nodePtr != NULL) //while loop prints the data in current node pointed by node pointer until null
{
printf("%d", nodePtr->data); //print the node pointer data
nodePtr = nodePtr->next; //node pointer to point the next node in linked list
if(nodePtr != NULL) //prints the data in node pointer until null
printf("-->"); //visually separate the values by easy reading
}
}
int main() //beginning of main function
{
int i, Count1, Count2, temp; //variable declaration
printf("Enter number of nodes in first Linked List : \n"); //print entered number of nodes in first link list by user
fflush(stdout); //clear buffered data in output
scanf("%d", &Count1); //scan entered number of nodes by user
printf("Enter %d numbers : \n", Count1); //print entered number by user
for(i=0; i<Count1; i++) //for loop increment the i value less than count 1
{
scanf("%d", &temp); //scan incremented value
insert_node(&head_pointer1, temp); //insert new node in linked list pointed by head pointer 1
}
printf("\nEnter number of nodes in second Linked List : \n"); //print entered number of nodes in second link list by user
fflush(stdout); //clear buffered data in output
scanf("%d", &Count2); //scan entered number of nodes by user
printf("Enter %d numbers : \n", Count2); //print entered number by user
for(i=0; i<Count2; i++) //for loop increment the i value less than count 2
{
scanf("%d", &temp); //scan incremented value
insert_node(&head_pointer2, temp); //insert new node in linked list pointed by head pointer 2
}
get_union(head_pointer1, head_pointer2); //getting Union of two given linked list to display
get_intersection(head_pointer1, head_pointer2); //getting intersection of two given linked list to display
printf("\nUnion Linked List\n"); //print union linked list output
printLinkedList(head_union); //print linked list head union
printf("\nIntersection Linked List\n"); //print intersection linked list output
printLinkedList(head_intersection); //print linked list head union
return 0; //return value 0 to indicating successful execution
}

