Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
LIVE BATCH 9
PROJECT 1
Aim ;
To Create a user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
- 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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Link list node
struct Node {
int data;
struct Node* next;
};
//function declaration
void push(struct Node** head_ref, int new_data);
int isPresent(struct Node* head, int 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;
struct Node *t1 = head1, *t2 = head2;
// Insert all elements of list1 to the result list
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
{
push(&result, t2->data);
}
t2 = t2->next;
}
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;
struct Node* t1 = head1;
// 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)
{
if (isPresent(head2, t1->data))
{
push(&result, t1->data);
}
t1 = t1->next;
}
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
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)
{
printf("%d ", node->data);
node = node->next;
}
}
// 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;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
// Driver program to test above function
int main()
{
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d", &n, &m);
// Starth structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link list1
for(int i=0;i<n;i++)
{
printf("Enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp);
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("Enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp);
}
//call the function of intersection
intersecn = getIntersection(head1, head2);
//call the function of union
unin = getUnion(head1, head2);
printf("\n First list is ");
//function calling with structure pointer as argument
printList(head1);
printf("\n Second list is ");
printList(head2);
printf("\n Intersection list is ");
printList(intersecn);
printf("\n Union list is ");
printList(unin);
return 0;
}
Figure shows the program and output obtained




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
- a,b 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 interfacces for working with set using Linked list
- Union and intersection of sets is found with the help of program.
- Moorthi Shi brother can you please give me your cantact no,are you stay in chennai?