Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
CHALLENGE 8 : PROJECT
//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)
Overall, this program demonstrates how to create sets using linked lists and perform set operations such as union and intersection. It provides a basic framework that can be extended to include additional set operations or error handling as needed. 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)
· The goal of this program is to demonstrate the implementation of mathematical set theory operations, specifically the union and intersection, using linked lists in the C programming language. The program allows the user to input two sets represented as lists of data, and then performs the union and intersection operations on these sets.
- By providing a working example of set operations, the program aims to showcase the principles of set theory and how they can be applied in a programming context. It illustrates how linked lists can be used to represent sets and how various set operations can be implemented using these data structures.
- Additionally, the program serves as a starting point for further development and exploration of set theory concepts. Users can build upon this foundation to implement additional set operations, such as set difference or complement, or enhance the program with additional features and error handling as needed.
Key Highlights of the Project: (4 to 5 point)
- We create an empty set using the createSet() function.
- We add elements to the set using the addElement() function.
- We check if the set contains the element using the isElementPresent() function, and it returns 1, indicating that the element is present in the set.
- We check if the set contains the element using the isElementPresent() function, and it returns 0, indicating that the element is not present in the set.
- We remove the element from the set using the removeElement() function.
- We check if the set contains the element again using the isElementPresent() function, and it returns 0, indicating that the element is not present in the set anymore.
Deliverables of this project: (4 to 5 points)
The deliverables of this program include:
- Source Code: The program source code written in C, which includes the implementation of set operations such as creating sets, adding elements, removing elements, checking element presence, printing sets, performing set union, and set intersection.
- Executable Program: After compiling the source code, an executable program can be generated that allows users to interact with the set operations. Users can input two sets and obtain the union and intersection results.
- Functionality: The program provides the functionality to create sets, add elements to sets, remove elements from sets, check if an element is present in a set, print sets, perform set union, and perform set intersection. These features allow users to manipulate sets and perform basic set theory operations.
- Output: The program outputs the results of set operations, specifically the union and intersection of the two sets provided by the user. It displays the final sets after the operations are performed.
- Demonstration: The program serves as a demonstration and example of implementing set operations using linked lists in C. It can be used as a learning resource to understand the implementation details of set theory operations and how they can be applied in a programming context.
Expected Outcomes: (4 to 5 points)
The expected outcome of this program, based on the provided implementation, would be as follows:
- The program prompts the user to input two sets, which are represented as lists of data.
- The program displays the original sets entered by the user.
- The program performs the union operation on the two sets and displays the resulting set, which contains all unique elements from both sets.
- The program performs the intersection operation on the two sets and displays the resulting set, which contains only the common elements present in both sets.
- The program terminates and exits.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Set {
struct Node* head;
};
void createSet(struct Set* set) {
set->head = NULL;
}
int isEmpty(struct Set* set) {
return set->head == NULL;
}
void addElement(struct Set* set, int element) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = element;
newNode->next = NULL;
if (isEmpty(set)) {
set->head = newNode;
} else {
struct Node* current = set->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void removeElement(struct Set* set, int element) {
if (isEmpty(set)) {
return;
}
struct Node* current = set->head;
struct Node* prev = NULL;
while (current != NULL && current->data != element) {
prev = current;
current = current->next;
}
if (current == NULL) {
return;
}
if (prev == NULL) {
set->head = current->next;
} else {
prev->next = current->next;
}
free(current);
}
int isElementPresent(struct Set* set, int element) {
struct Node* current = set->head;
while (current != NULL) {
if (current->data == element) {
return 1;
}
current = current->next;
}
return 0;
}
void printSet(struct Set* set) {
if (isEmpty(set)) {
printf("Set is empty.\n");
return;
}
struct Node* current = set->head;
printf("Set: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
struct Set setUnion(struct Set* set1, struct Set* set2) {
struct Set resultSet;
createSet(&resultSet);
struct Node* current = set1->head;
while (current != NULL) {
addElement(&resultSet, current->data);
current = current->next;
}
current = set2->head;
while (current != NULL) {
if (!isElementPresent(&resultSet, current->data)) {
addElement(&resultSet, current->data);
}
current = current->next;
}
return resultSet;
}
struct Set setIntersection(struct Set* set1, struct Set* set2) {
struct Set resultSet;
createSet(&resultSet);
struct Node* current = set1->head;
while (current != NULL) {
if (isElementPresent(set2, current->data)) {
addElement(&resultSet, current->data);
}
current = current->next;
}
return resultSet;
}
int main() {
struct Set set1, set2;
createSet(&set1);
createSet(&set2);
// Add elements to set1
addElement(&set1, 10);
addElement(&set1, 20);
addElement(&set1, 30);
addElement(&set1, 40);
addElement(&set1, 50);
// Add elements to set2
addElement(&set2, 20);
addElement(&set2, 30);
addElement(&set2, 40);
addElement(&set2, 50);
addElement(&set2, 60);
// Print the sets
printf("\nElements in First ");
printSet(&set1);
printf("\nElements in Second ");
printSet(&set2);
// Perform union
struct Set unionSet = setUnion(&set1, &set2);
printf("\nUnion ");
printSet(&unionSet);
// Perform intersection
struct Set intersectionSet = setIntersection(&set1, &set2);
printf("\nIntersection Set: ");
printSet(&intersectionSet);
return 0;
}
Output:
Elements in First Set: 10 20 30 40 50
Elements in Second Set: 20 30 40 50 60
Union Set: 10 20 30 40 50 60
Intersection Set: Set: 20 30 40 50