Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
#include <stdio.h>
#include <stdlib.h>
// Structure of linked list node for data and address of next link
struct node
{
int data; // variable to store data
struct node *next; // pointer variable to store address of next link
} *first_linked_list, *second_linked_list, *union_list, *intersection_list; // declaring required variables
//function to insert a node in front of singly linked list
void insert_node(struct node **head, int num) // function with 2 arguments first as head and second as num
{
struct node* new_node = (struct node*) malloc(sizeof(struct node)); // creating new node in linked list
new_node->data = num; // inserting num in data of the node
new_node->next = *head; // next pointer will point to head node
*head = new_node; // making new_node as head of linked list
}
// function with 2 arguments to traverse through linked list and search for element
int search_element(struct node *head, int num)
{
while (head != NULL) // while condition to enter in loop for traversing
{
if (head->data == num) // if condition to check if data in head is equal to num
{
return 1; // return 1 if true (which will act as a true for reference taken)
}
head = head->next; // making head to head next for checking adjacent node
}
return 0; // if num not found return 0 (which will act as a false for reference taken)
}
struct node* find_union(struct node *first_linked_list, struct node *second_linked_list) // returns the union of 2 given linked list
{
union_list = NULL; // initializing with NULL
struct node *temp = first_linked_list; // initializing temp pointer with first_linked_list used for traversal
while(temp != NULL) // while condition to traverse in and add node to union_list
{
insert_node(&union_list, temp->data); // calling insert_node function to add required node in union_list
temp = temp->next; // making temp as temp next for while condition
}
//inserting second_linked_list nodes which are absent in first_linked_list
while(second_linked_list != NULL) // while condition for second_linked_list traversal
{
if(!search_element(first_linked_list, second_linked_list->data)) // if condition to search_element element in second_linked_list
{
insert_node(&union_list, second_linked_list->data); // if element not found calling insert function to add in union_list
}
second_linked_list = second_linked_list->next; // making second_linked_list as second_linked_list next for while condition and traverse further
}
return union_list; // return union_list for printing
}
struct node* find_intersection(struct node *first_linked_list, struct node *second_linked_list) // used to return common nodes of linked list i.e. intersection_list
{
intersection_list = NULL; // initializing intersection_list to NULL
//Search every element of first_linked_list in second_linked_list, If found then add it to intersection List
while(first_linked_list != NULL) // while condition to enter while loop i.e. first_linked_list is not empty
{
if(search_element(second_linked_list, first_linked_list->data)) // if condition to add element in intersection_list i.e. calling search_element to check common elements
{
insert_node(&intersection_list, first_linked_list->data); // calling insert_node to add common node data in intersection_list
}
first_linked_list = first_linked_list->next; // making first_linked_list point to adjacent node for continuing traversal
}
return intersection_list;
}
void print_linked_list(struct node *node_ptr) // function to print linked list from head to tail node
{
while (node_ptr != NULL) // while condition to traverse linked list
{
printf("%d", node_ptr->data); // printing data at current node
node_ptr = node_ptr->next; // making node_ptr to point adjacent or next node
if(node_ptr != NULL) // if condition to print "-->" showing link between nodes
printf("-->");
}
}
int main()
{
int first_list_size, second_list_size, temp; // declaring required variables
first_linked_list = second_linked_list = NULL; // initializing first_linked_list & second_linked_list to NULL
printf("Number of nodes in first Linked List: "); // instruction for user to enter required details
scanf("%d", &first_list_size); // taking size of Linked list from user
printf("Enter %d numbers: \n", first_list_size); // instruction for user to enter required details
for(int i = 0; i<first_list_size; i++) // for loop used to traverse and insert_node
{
scanf("%d", &temp); // taking user input for element to add
insert_node(&first_linked_list, temp); // calling insert_node function to add data in first_linked_list
}
printf("First Linked List: "); // printing First Linked List
print_linked_list(first_linked_list); // print the linked list by calling print_linked_list function
printf("\n\nNumber of nodes in second Linked List: "); // instruction for user to enter required details
scanf("%d", &second_list_size); // taking size of Linked list from user
printf("Enter %d numbers: \n", second_list_size); // instruction for user to enter required details
for(int i = 0; i<second_list_size; i++) // for loop used to traverse and insert_node
{
scanf("%d", &temp); // taking user input for element to add
insert_node(&second_linked_list, temp); // calling insert_node function to add data in first_linked_list
}
printf("Second Linked List: "); // taking user input for element to add
print_linked_list(second_linked_list); // print the linked list by calling print_linked_list function
//printing Union of two given linked list
find_union(first_linked_list, second_linked_list); // printing union_list i.e. union of 2 linked list
find_intersection(first_linked_list, second_linked_list); // printing intersection_list i.e. intersection of 2 linked list
printf("\n\nUnion of given 2 Linked Lists:\n"); // priting Union of 2 linked list
print_linked_list(union_list); // calling print_linked_list for priting union_list
printf("\n\nIntersection of given 2 Linked List:\n"); // priting intersection of 2 linked list
print_linked_list(intersection_list); // calling print_linked_list for priting intersection_list
return 0;
}