Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <iostream>
// Node structure for the linked list
struct Node {
int data;
Node* next;
};
// Set class implemented using a linked list
class Set {
private:
Node* head; // Pointer to the head of the linked list
public:
// Constructor
Set() : head(nullptr) {}
// Destructor
~Set() {
// Free the memory allocated for the linked list
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
}
// Function to add an element to the set
void add(int element) {
// Check if the element is already present in the set
if (contains(element)) {
std::cout << "Element " << element << " is already present in the set.\n";
return;
}
// Create a new node
Node* newNode = new Node;
newNode->data = element;
newNode->next = nullptr;
// If the set is empty, make the new node as the head
if (head == nullptr) {
head = newNode;
return;
}
// Traverse the linked list to find the last node
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
// Add the new node at the end of the linked list
current->next = newNode;
}
// Function to remove an element from the set
void remove(int element) {
// If the set is empty, return
if (head == nullptr) {
std::cout << "Set is empty. Cannot remove element.\n";
return;
}
// If the element to be removed is the head, update the head pointer
if (head->data == element) {
Node* temp = head;
head = head->next;
delete temp;
std::cout << "Element " << element << " removed from the set.\n";
return;
}
// Traverse the linked list to find the node containing the element
Node* current = head;
while (current->next != nullptr && current->next->data != element) {
current = current->next;
}
// If the element is found, remove it from the set
if (current->next != nullptr) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
std::cout << "Element " << element << " removed from the set.\n";
} else {
std::cout << "Element " << element << " is not present in the set.\n";
}
}
// Function to check if an element is present in the set
bool contains(int element) {
// Traverse the linked list to find the element
Node* current = head;
while (current != nullptr) {
if (current->data == element) {
return true;
}
current = current->next;
}
return false;
}
// Function to display the elements in the set
void display() {
std::cout << "Set: ";
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
int main() {
// Create a set and perform operations
Set set;
set.add(5);
set.add(10);
set.add(15);
set.add(10); // Adding duplicate element
set.display();
set.remove(10);
set.remove(20); // Removing non-existent element
set.display();
std::cout << "Set contains 5: " << (set.contains(5) ? "true" : "false") << std::endl;
std::cout << "Set contains 20: " << (set.contains(20) ? "true" : "false") << std::endl;
return 0;
}