Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
To achieve this, you can create a user-defined data type for the Set using a Linked List.
The user should be able to input two lists of data, and the program will provide the union and intersection of these two sets.
#include
#include
// Define the Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
// Create a new Node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Insert a Node at the end of the Linked List
void insertNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Display the elements of the Linked List
void displayList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to compute the union of two sets
Node* setUnion(Node* set1, Node* set2) {
Node* result = NULL;
Node* temp1 = set1;
Node* temp2 = set2;
while (temp1 != NULL) {
insertNode(&result, temp1->data);
temp1 = temp1->next;
}
while (temp2 != NULL) {
if (!contains(result, temp2->data)) {
insertNode(&result, temp2->data);
}
temp2 = temp2->next;
}
return result;
}
// Function to compute the intersection of two sets
Node* setIntersection(Node* set1, Node* set2) {
Node* result = NULL;
Node* temp1 = set1;
while (temp1 != NULL) {
if (contains(set2, temp1->data)) {
insertNode(&result, temp1->data);
}
temp1 = temp1->next;
}
return result;
}
// Function to check if a value is present in the set
int contains(Node* set, int value) {
Node* temp = set;
while (temp != NULL) {
if (temp->data == value) {
return 1;
}
temp = temp->next;
}
return 0;
}
// Function to free the memory allocated for the Linked List
void freeList(Node** head) {
Node* temp = *head;
while (temp != NULL) {
Node* nextNode = temp->next;
free(temp);
temp = nextNode;
}
*head = NULL;
}
int main() {
Node* set1 = NULL;
Node* set2 = NULL;
// Example usage
insertNode(&set1, 1);
insertNode(&set1, 2);
insertNode(&set1, 3);
insertNode(&set2, 2);
insertNode(&set2, 3);
insertNode(&set2, 4);
printf("Set 1: ");
displayList(set1);
printf("Set 2: ");
displayList(set2);
Node* unionSet = setUnion(set1, set2);
printf("Union: ");
displayList(unionSet);
Node* intersectionSet = setIntersection(set1, set2);
printf("Intersection: ");
displayList(intersectionSet);
// Free the memory allocated for the Linked Lists
freeList(&set1);
freeList(&set2);
freeList(&unionSet);
freeList(&intersectionSet);
return 0;
}
In this code, we define a Node structure representing a node in the Linked List. The createNode() function creates a new node with the given data, and the insertNode() function inserts a node at the end of the Linked List. The displayList() function prints the elements of the Linked List.
The setUnion() function computes the union of two sets by iterating over the elements of both sets and adding them to the result set. The setIntersection() function computes the intersection of two sets by iterating over the elements of the first set and checking if each element is present in the second set.
The contains() function checks if a value is present in the set by iterating over the elements of the set and comparing the values.
In the main() function, we create two sets, set1 and set2, by inserting nodes into the Linked Lists. We then compute the union and intersection of the sets using the setUnion() and setIntersection() functions. Finally, we display the resulting sets.