Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdio.h>
struct Node
{
int data;
struct Node* next;
};
//Defining function 'insert' to add numbers in the linked list from the user
void insert(struct Node** head, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //Creates a new node
newNode->data = data; //Stores the data part
newNode->next = NULL; //Assigns NULL to the address of newnode
if (*head == NULL) //Condition to check if the head is NULL then it will store the address of the new node (Happens only 1st time)
{
*head = newNode;
}
else //Condition to restore the value of head in temp, and then connecting new node with previous node using temp pointer
{
struct Node* temp = *head; //Assigning head value to temp
while (temp->next != NULL) //While loop will run/trace till temp's address is NULL
{
temp = temp->next; //Temp will store the address of that node
}
temp->next = newNode;
}
}
//Defining function display to print the linked list/Set
void display(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
//Defining function Union() which takes two sets as arguments and returns only the unique numbers present in both sets
struct Node* Union(struct Node* set1, struct Node* set2)
{
struct Node* result = NULL; //Declaring result to store the union of sets
struct Node* temp1 = set1; //Sets the value of temp1 to the 1st number of set1
struct Node* temp2 = set2; //Sets the value of temp2 to the 2nd number of set2
while (temp1 != NULL)
{
insert(&result, temp1->data); //Stores value of set1 elements in result
temp1 = temp1->next;
}
while (temp2 != NULL)
{
if (!find(result, temp2->data)) //Searches for any repetative number in set1
{
insert(&result, temp2->data); //If not then element is stored in result.
}
temp2 = temp2->next;
}
return result; //At the end the result is returned with all the unique elements present in both sets
}
//Defining function Intersection() which takes two sets(1 and 2) as argument and returns the intersecting elements from both sets
struct Node* Intersection(struct Node* set1, struct Node* set2)
{
struct Node* result = NULL; //Declaring result to store the intersecting of sets
struct Node* temp1 = set1; //Sets the value of temp1 to the 1st number of set1
while (temp1 != NULL) {
if (find(set2, temp1->data)) //Comparing set2 elements values with set1 elements
{
insert(&result, temp1->data); //If yes, then elements are stored in result
}
temp1 = temp1->next;
}
return result; //At the end the result is returned with all the common elements present in both sets
}
//Defining integer datatype function find() with two arguments to search for elements that are same in both sets
int find(struct Node* head, int data)
{
struct Node* temp = head; //Declaring temp as head
while (temp != NULL) //This loop will run till the temp reaches the last element of the set
{
if (temp->data == data) //Checking if elements are equal
{
return 1; //If yes it returns 1 else it returns 0;
}
temp = temp->next; //Jumps to the next element
}
return 0;
}
int main()
{
//Declaring two struct node datatype set1 and set2 to store values in them
struct Node* set1 = NULL;
struct Node* set2 = NULL;
int num1, num2, input; //Initialising integer datatype variables num1, num2, and input.
printf("Enter the number of elements for List 1: "); //Taking input from the user for no. of elements and list of elements in the set
scanf("%d", &num1);
printf("Enter the elements for Set 1: ");
for (int i = 0; i < num1; i++)
{
scanf("%d", &input);
insert(&set1, input); //Calling function insert where the values are stored in set1
}
printf("Enter the number of elements for Set 2: ");
scanf("%d", &num2);
printf("Enter the elements for Set 2: ");
for (int i = 0; i < num2; i++)
{
scanf("%d", &input);
insert(&set2, input); //Calling function insert where the values are stored in set1
}
printf("Set 1: ");
display(set1); //Calling function display() to print the values of set1
printf("Set 2: ");
display(set2); //Calling function display() to print the values of set2
struct Node* unionSet = Union(set1, set2); //Calling function Union
printf("Union: ");
display(unionSet); //Printing list of union set
struct Node* intersectionSet = Intersection(set1, set2); //Calling function intersection
printf("Intersection: ");
display(intersectionSet); //Printing list of intersection set
return 0;}