Overview of the Project:
In this project the mathematical set theory is implemented using c programing. The code is writtern in such a way that from the user side two sets can be given, and also the operation to be performed on the given sets can also be selected by the user as per their choice.The program gives output as per users choice ie, either union or intersection operation will performed on the given sets.
Goals:
1.Implementing mathematical set theory using linked list.
2.Perform operation on given set as per users choice
Key Highlights of the Project:
1.A node is created
2.Creating two linked list using node
3.Two linked lists were named as set1 and set2
4. Union of sets and Intersection of sets were found according to user choice by function calls
5.To create linked list for set, displaying the created set, finding union of two sets, find intersection of two sets., all these done by means of seperate funnction calls from the main
Deliverables of the Project:
- Intersection of data of two list.
- Union of data of two list
c code
//Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdio.h>//standard nput output libray
#include<stdlib.h>//for dynamic memmory allocation
//a node in a linkedlist is defined using structure.
struct node// name of structure is node
{
int data;//integer type variable is defined for storing data part of node and named as data
struct node* next_link_address;//pointer to next node named next_link_address, which is a self referential structure
};
struct node* create_set(int n)//function defnition for creating set with one parameter(no of elements in set), its return type is structnode since it returns head of the linked list
{
struct node* head=NULL;//pointer named head is created to store address of first node, initialized to null
struct node* temp=NULL;//pointer used to create indivitual node in the linked list
struct node* pointer;// to itrate through the linked list to insert node t correct position
for(int i=0;i<n;i++)//for loop to itrate n no of times for n elements in set
{
temp=(struct node*)malloc(sizeof(struct node*));//first node creation by allocating memmory using malloc
printf("Element %d : ",i+1);//ask user for data of first node, ie first element of set
scanf("%d",&(temp->data));//first element received from user is saved
temp->next_link_address=NULL;//address part of forst node
//this may be our first node or our last node, head is null means linked list is empty then make head as newly created node.
//otherwise need to add new data at the end of the linkedlist
if(head==NULL)//if list is empty
{
head=temp;
}
else
{
pointer=head;//to add new data at the end of the linked list using 'pointer', it starts from the begining
//while loop to itrate the 'pointer' till the end of the list
while (pointer->next_link_address!=NULL) //when next link address in not null while loop will excecuted
pointer=pointer->next_link_address;//loop runs till pointer reaches last node
//when reaches last node the next_link_address part becomes null and loopp breaks
pointer->next_link_address=temp;//then temp is added to th eend of the pointer
}
}
return head;//starting address is returned
}
//function to display the created set
void display_set(struct node* head)//starting address is passed as function parameter
{
struct node *pointer =head;//head stores the address of first node, is assigned to pointer. pointer will start at head
printf("{");//opening brace of set to display in console
while(pointer!= NULL)// while loop checks pointer points to null or not
{
printf(" %d ",pointer->data);//if pointer not equal to null the value in p is printed
pointer=pointer->next_link_address;//pointer will go to the next node
} //this loop continues till pointer equal null
printf("}");//closing brace of set to show in console
}
//function to make union of two sets
struct node* union_of_sets(struct node* head1, struct node* head2) //starting address of two sets were passed as parameters
{
struct node* result = NULL;//variable result, for storing the union of two sets is declared of type struct node and initialized to null
struct node* temp;//variable named temp of type struct node to store the linked list formed temporarly as a pointer variable
struct node* pointer;//variable named pointer of type struct node to point each node as itrate over the list
// Traverse the first list and add all its nodes to the result list
pointer = head1;//value of head1 the first node of set1 is assigned to the pointer variable
while (pointer != NULL)//checking condition in while loop,if head1 is null or not
{
temp = (struct node*)malloc(sizeof(struct node));//first node is created and memmory is allocated using malloc
temp->data = pointer->data;//first element of set 1 is assigned to temp
temp->next_link_address = NULL;//address part of the element 1 of set 1 is initialized as null
if (result == NULL) // if loop checks the result is null or not, willl be null for first
{
result = temp;//for null value of result the temp variable data is assigned to result variable
}
else
{
struct node* p = result;//if result not equat to null the the value in result is stored to another variable
while (p->next_link_address != NULL)//while loop checksthe address part of p is null or not and excecuteswhile loop for address part not equal to null
{
p = p->next_link_address;//the address oart of p is assigned to p
}
p->next_link_address = temp;//if p's address part is null then the temp variable is assigned toaddress part of p
}
pointer = pointer->next_link_address;// address part of pouinter is assigned to pointer variable if p's adress part is not null
}
// Traverse the second list and add all its nodes to the result list
pointer = head2;//head to, first element of set2 is assigned to pointer
while (pointer != NULL)//while loop runs for pointer not equal to null
{
temp = (struct node*)malloc(sizeof(struct node));//memmory allocated for the first node tin the linked list
temp->data = pointer->data;//data pointedby the pounter (head of the list)is assigned to temp
temp->next_link_address = NULL;//address part of the temp's first node is set to null
if (result == NULL) //using if condition checking result is null or not
{
result = temp;//temp is assigned to result
}
else //else part for result not equal to null
{
struct node* p = result;//second node element onwards, ie. when result not equalt o null the result value is assigned to the pointer p
while (p->next_link_address != NULL)//while loop for checking the address part of p is null or not
{
p = p->next_link_address;//the value of address part of p is assigned to p for while condition fails
}
p->next_link_address = temp;//the value of temp is assigned to the address of next link
}
pointer = pointer->next_link_address;//address part of pointer is assigned to pointer variable
}
return result;//the function returns variable result in which new set created is stored
}
//function to find intersection of two sets
struct node* intersection_of_sets(struct node* head1, struct node* head2) //the first element of two sets were passed as arguments and the function returns struct node type
{
struct node* result = NULL;//result variable is declared of type struct node as initialized as null
struct node* temp;//temp variable is declared of type struct node for storing each value of set while creating
struct node* pointer1;//variable to itrate through set 1
struct node* pointer2;//variable to itrate through set 2
// Traverse the first list
pointer1 = head1;//first element of set 1 is assigned to pointer 1
while (pointer1 != NULL)//while loop runs for pointer1 ie the first element not equal to null
{
// For each element in the first list, traverse the second list
pointer2 = head2;//second element of set 2 is assigned to pointer 2
while (pointer2 != NULL) //while loop runs for pointer2 ie the first element not equal to null
{
// If the current element is present in both lists, add it to the result list
if (pointer1->data == pointer2->data)//checking both the elements is same or not by means of if loop
{
temp = (struct node*)malloc(sizeof(struct node));//if loop returns true then malloc allocates memmory for first node of new linked list created to store intersection of two sets
temp->data = pointer1->data;//data part is stored to temp's data
temp->next_link_address = NULL;//temps address part is set to null
if (result == NULL)//if condition checks for result is null or not
{
result = temp;//excecutes this ststement for reult is null
}
else// else part for result not equql to null
{
struct node* p = result;//result value is stored to pointer variable p
while (p->next_link_address != NULL) //while loop checks for address part of p is null or not
{
p = p->next_link_address;//if address part not equal to null assigns the address to p variable
}
p->next_link_address = temp;//wile loop is false then temp variable is assigned to the address part of the p
}
break;// while loop breaks
}
pointer2 = pointer2->next_link_address;// the address part of the pointer 2 is assigned to the pointer 2
}
pointer1 = pointer1->next_link_address;// the address part of the pointer 1 is assigned to the pointer 1
}
return result;//the result variable is returned
}
int main()// main function starts
{
int n1,n2;// two integer variablesn1 and n2 declared to store the no of elements of set1 and set2 is declared
int choice;//varable of integer type to store the user choice of set operation
struct node* head1 = NULL;//to store the head of first linked list(set1 ) a pointer to struct node is created
struct node* head2 = NULL;//to store the head of second linked list(set2) a pointer to struct node is created
printf("No of elements in SET1:");//display message to user to get the no of elements in the set 1 from user
scanf("%d",&n1);// the value entered by the user is saved in the variable n1
head1=create_set(n1);// a function named create_set is called with no of elements entered for set 1 by user as argument
printf("No of elements in SET2 :");//display message to user to get the no of elements in the set 2 from user
scanf("%d",&n2);// the value entered by the user is saved in the variable n2
head2=create_set(n2);// a function named create_set is called with no of elements entered for set 2 by user as argument
printf("\n\nSET1 = ");//dispaly message in console
display_set(head1);// the function named display_set is called with first element of set 1 as parameter
printf("\nSET2 = ");//dispaly message in console
display_set(head2);// the function named display_set is called with first element of set 2 as parameter
printf("\n \n Which SET operation you want to perform? \n (1) UNION \n (2) INTERSECTION \n");//dispaly message in console for the user to choose between two set operations listed
printf("Enter your choice ('1' or '2') : ");//prompt the user to enter their choice either 1 or 2
scanf("%d",&choice); // reads the users choice fronm the console
switch(choice)//users choice, stored in variable choice is passed to the switch case statement
{
case 1://for users selection is 1
printf(" SET1 union SET2 = ");//display message in console
struct node* union_of_set = union_of_sets(head1, head2);//union_of_set function is called with head 1 and head 2 as parameters and the return value is saved to the union of sets pointer
display_set(union_of_set);//function named display_set is called with union_of_set as parameter
break;//break for case 1
case 2://for users selection is 2
printf(" SET1 intersection SET2 = ");//display message in console
struct node* intersection_of_set = intersection_of_sets(head1, head2);//intersection_of_set function is called with head 1 and head 2 as parameters and the return value is saved to the intersection_of_set pointer
display_set(intersection_of_set);//function named display_set is called with intersection_of_set as parameter
break;//break for case 2
case 3:// for users choice other than 1 or 2
printf("INVALID CHOICE");//display message in console
break;// break statement
}
return 0;//end of main function
}
Here in this program the code prompts user to enter the no of elements in set 1. The data entered by the user is used to call the create set function. when create_set function is called it will ask the user to enter the no of elements for set as given earlier. these elements were saved to a linked list and returned to the main function. Then using the same create_set function set 2 also created similarly.
after that both set1 and set 2 were displayed in the console by means of display_set function.
Then the user is asked to choose the operation to be performed on the set
A choice is given to the user like choose 1 for union of set to be performed and 2 to perform intersection of two sets.
As per user choice , by means of switch case the choice is excecuted.
if user choose 1 , ie union of sets then the case 1 of switch case statement finds the union of 2 sets by calling a function to find the union of sets named union_of_sets . the result of union of two sets is displayed again by calling the dispaly_set function.. similarly if user go for intersection of sets,the case 2 of switch case staement done the task.under case 2 of switch case another function named intersection_of_sets called for finding the intersection of two sets and doutcome of intersection is displayed by using display_set.
Expected outcomes:
Two basic mathematical operation is perfomed using two sets. Union and intersection. Union of 2 set means tge resulting set contains all the elements in both sets. intersection of two set means the resulting set contains the elements common to both sets. the screen shot of output is attached below