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
#include <stdio.h>
#include <stdlib.h>
// Define the node structure for the linked list
struct Node
{
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the linked list
void insertNode(struct Node** head, int data)
{
struct Node* newNode = createNode(data);
if (*head == NULL)
{
*head = newNode;
}
else
{
struct Node* temp = *head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to perform the union of two sets
struct Node* findUnion(struct Node* set1, struct Node* set2)
{
struct Node* result = NULL;
// Traverse set1 and insert its elements into the result set
struct Node* temp1 = set1;
while (temp1 != NULL)
{
insertNode(&result, temp1->data);
temp1 = temp1->next;
}
// Traverse set2 and insert its elements into the result set (if not already present)
struct Node* temp2 = set2;
while (temp2 != NULL)
{
int found = 0;
struct Node* tempResult = result;
while (tempResult != NULL)
{
if (tempResult->data == temp2->data)
{
found = 1;
break;
}
tempResult = tempResult->next;
}
if (!found) {
insertNode(&result, temp2->data);
}
temp2 = temp2->next;
}
return result;
}
// Function to perform the intersection of two sets
struct Node* findIntersection(struct Node* set1, struct Node* set2)
{
struct Node* result = NULL;
// Traverse set1 and check if its elements are also present in set2, if so, insert into the result set
struct Node* temp1 = set1;
while (temp1 != NULL)
{
struct Node* temp2 = set2;
while (temp2 != NULL)
{
if (temp1->data == temp2->data)
{
insertNode(&result, temp1->data);
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
return result;
}
// Function to display the elements of a linked list
void displayList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to free the memory allocated for the linked list
void freeList(struct Node* head)
{
struct Node* temp;
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
int main()
{
struct Node* set1 = NULL;
struct Node* set2 = NULL;
// Read input for set1
int n1, data,i;
printf("Enter the number of elements in Set 1: ");
fflush(stdout);
scanf("%d", &n1);
printf("Enter the elements of Set 1: ");
for (i = 0; i < n1; i++)
{
fflush(stdout);
scanf("%d", &data);
insertNode(&set1, data);
}
// Read input for set2
int n2;
printf("Enter the number of elements in Set 2: ");
fflush(stdout);
scanf("%d", &n2);
printf("Enter the elements of Set 2: ");
for (i = 0; i < n2; i++)
{
fflush(stdout);
scanf("%d", &data);
insertNode(&set2, data);
}
// Find and display the union and intersection of the two sets
struct Node* unionResult = findUnion(set1, set2);
printf("Union of the two sets: ");
displayList(unionResult);
struct Node* intersectionResult = findIntersection(set1, set2);
printf("Intersection of the two sets: ");
displayList(intersectionResult);
// Free the memory allocated for the linked lists
freeList(set1);
freeList(set2);
freeList(unionResult);
freeList(intersectionResult);
return (0);
}
