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
PROGRAM :-
#include
#include
// Structure to represent an element in the set
typedef struct Node {
int data;
struct Node* next;
} Node;
// Structure to represent the set
typedef struct Set {
Node* head;
} Set;
// Function to create a new Node with the given data value
Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
// Function to add an element to the set
void addElement(Set* set, int data) {
Node* node = createNode(data);
if (set->head == NULL) {
set->head = node;
} else {
Node* current = set->head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
// Function to compute the union of two sets
Set* setUnion(Set* set1, Set* set2) {
Set* result = (Set*)malloc(sizeof(Set));
result->head = NULL;
Node* current = set1->head;
while (current != NULL) {
addElement(result, current->data);
current = current->next;
}
current = set2->head;
while (current != NULL) {
Node* temp = result->head;
while (temp != NULL) {
if (temp->data == current->data) {
break;
}
temp = temp->next;
}
if (temp == NULL) {
addElement(result, current->data);
}
current = current->next;
}
return result;
}
// Function to compute the intersection of two sets
Set* setIntersection(Set* set1, Set* set2) {
Set* result = (Set*)malloc(sizeof(Set));
result->head = NULL;
Node* current = set1->head;
while (current != NULL) {
Node* temp = set2->head;
while (temp != NULL) {
if (temp->data == current->data) {
addElement(result, current->data);
break;
}
temp = temp->next;
}
current = current->next;
}
return result;
}
// Function to print the elements of a set
void printSet(Set* set) {
Node* current = set->head;
printf("{ ");
fflush(stdout);
while (current != NULL) {
printf("%d ", current->data);
fflush(stdout);
current = current->next;
}
printf("}\n");
}
int main() {
// Get input from user
Set* set1 = (Set*)malloc(sizeof(Set));
set1->head = NULL;
Set* set2 = (Set*)malloc(sizeof(Set));
set2->head = NULL;
int num1, num2;
printf("Enter the number of elements in set 1:\n");
fflush(stdout);
scanf("%d", &num1);
printf("Enter the elements of set 1:\n");
fflush(stdout);
for (int i = 0; i < num1; i++) {
int element;
scanf("%d", &element);
addElement(set1, element);
}
printf("Enter the number of elements in set 2:\n");
fflush(stdout);
scanf("%d", &num2);
printf("Enter the elements of set 2:\n");
fflush(stdout);
for (int i = 0; i < num2; i++) {
int element;
scanf("%d", &element);
addElement(set2, element);
}
// Compute and print the union of the sets
Set* unionSet = setUnion(set1, set2);
printf("Union of the sets: ");
fflush(stdout);
printSet(unionSet);
// Compute and print the intersection of the sets
Set* intersectionSet = setIntersection(set1, set2);
printf("Intersection of the sets: ");
fflush(stdout);
printSet(intersectionSet);
// Free memory
Node* current = set1->head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
free(set1);
current = set2->head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
free(set2);
current = unionSet->head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
free(unionSet);
current = intersectionSet->head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
free(intersectionSet);
return 0;
}
OUTPUT :-
