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
Aim :
- The union of two sets A and B is the set of all those elements which are either in A or in B, i.e. A ∪ B, whereas the intersection of two sets A and B is the set of all elements which are common
- The intersection of these two sets is denoted by A ∩ B
Program code :
#include <stdio.h>
#include <stdlib.h>
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
};
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); //initalizes 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;
}
// 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;
}
// 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;
}
t = t->next; //transverse list data stores next input from user
}
return 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 "); //print the first list
//function calling with structure pointer as argument
printList(head1); //print list of head1
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 values
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;
}




OUTPUT :

Logic and Comments
- Linked List is a linear data structure
- Each node stores the data and the address of the next node
- Linked list is created of struct node which contains a data of integer datatype and address of the next node
- Inserting a node infront of linked list as void insert(struct Node** head_ref, int new_data); where **head denotes where the head is pointed and what is data stored
- Function to get union of two linked lists head1 and head2 with syntax
struct Node* get_Union(struct Node* head1, struct Node* head2) - Insert all elements of list1 to the result list using While loop, while the t1 is not equal to null and points to next data
- Similary with another while loop insert those elements of list2 which are not present in result list
- Function to get intersection of two linked lists head1 and head2 with syntax
struct Node* get_Intersection(struct Node* head1, struct Node* head2) - Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result using while loop
- To insert a node at the beginning of linked list using malloc with syntax struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); where memory allocation depends on the size of node
- The int data passed new_data is the new_node data and pointer of new_node will be pointed to the head of the linked list
- The head is then moved to the new node ,mark new node as the head of the linked list
- Function is printed to link list using the syntax void print_List(struct Node* node) , void is used as there is nothing to return while printing the linked list
- int is_Present(struct Node* head, int data) is the syntax used to search an element in linked list linearly traversing ,the element to be searched and the address of head pointed are defined here
- p,q and temp are initialized to be integer datatypes
- If head is NULL , node is empty otherwise the pointed variable at head points to the next one
- Always intialize the linked list with NULL inorder to avoid storing of any by default value to memory
- Started structure pointer with the empty list :struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersect = NULL;
struct Node* unin = NULL; - In main function , we are calling the function declared earlier
- First we enter the number of elements in both the list
- The values of linked list1 and linked list2 are entered with the help of for loops
- Call the function of intersection and union
- Function calling with structure pointer as argument is done and both the lists are printed also intersection and union of the list is found out throug this program
Result:
- Created a user define data to implement the user interfaces for working with set using Linked list
- Union and intersection of sets is found with the help of program.