Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview of the Project: (5 to 6 lines)
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists
Goals (2 to 3 points)
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key Highlights of the Project: (4 to 5 point)
- Creating two list of data using linked list method.
- Insert the node to 1st and 2nd list accordingly.
- Intersection of data of two lists.
- Getting Union of two list.
Deliverables of this project: (4 to 5 points)
- Intersection of data of two list.
- Union of data of two list
Expected Outcomes: (4 to 5 points)
- Student will get the Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// User-defined data type for a node in the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to check if the set is empty
bool isEmpty(Node* set) {
return (set == NULL);
}
// Function to check if an element exists in the set
bool contains(Node* set, int element) {
Node* current = set;
while (current != NULL) {
if (current->data == element) {
return true;
}
current = current->next;
}
return false;
}
// Function to add an element to the set
Node* add(Node* set, int element) {
if (!contains(set, element)) {
Node* newNode = createNode(element);
newNode->next = set;
return newNode;
}
return set;
}
// Function to remove an element from the set
Node* removeElement(Node* set, int element) {
if (set == NULL) {
return set;
}
if (set->data == element) {
Node* temp = set;
set = set->next;
free(temp);
return set;
}
Node* current = set;
while (current->next != NULL) {
if (current->next->data == element) {
Node* temp = current->next;
current->next = temp->next;
free(temp);
break;
}
current = current->next;
}
return set;
}
// Function to compute the union of two sets
Node* setUnion(Node* set1, Node* set2) {
Node* result = set1;
Node* current = set2;
while (current != NULL) {
if (!contains(result, current->data)) {
result = add(result, current->data);
}
current = current->next;
}
return result;
}
// Function to compute the intersection of two sets
Node* setIntersection(Node* set1, Node* set2) {
Node* result = NULL;
Node* current = set1;
while (current != NULL) {
if (contains(set2, current->data)) {
result = add(result, current->data);
}
current = current->next;
}
return result;
}
// Function to display the set
void displaySet(Node* set) {
if (set == NULL) {
printf("Set is empty.\n");
return;
}
printf("Set: ");
Node* current = set;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to free the memory allocated for the set
void freeSet(Node* set) {
Node* current = set;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
int main() {
Node* set1 = NULL;
Node* set2 = NULL;
// Add elements to the first set
set1 = add(set1, 5);
set1 = add(set1, 10);
set1 = add(set1, 15);
// Add elements to the second set
set2 = add(set2, 10);
set2 = add(set2, 20);
// Compute and display the union of the two sets
Node* unionSet = setUnion(set1, set2);
printf("Union of the sets: ");
displaySet(unionSet);
// Compute and display the intersection of the two sets
Node* intersectionSet = setIntersection(set1, set2);
printf("Intersection of the sets: ");
displaySet(intersectionSet);
// Free the memory allocated for the sets
freeSet(set1);
freeSet(set2);
freeSet(unionSet);
freeSet(intersectionSet);
return 0;
}
output:
