Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Note : For Better understanding word file is attached.

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

AIM : Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Program :

#include <stdio.h>

#include <stdlib.h>

// Node structure for the linked list

typedef struct Node {

int data;

struct Node* next;

} Node;

// Set structure using a linked list

typedef struct {

Node* head;

} Set;

// Function to create an empty set

Set createSet() {

Set set;

set.head = NULL;

return set;

}

// Function to check if the set is empty

int isEmpty(Set set) {

return set.head == NULL;

}

// Function to check if an element exists in the set

int contains(Set set, int element) {

Node* current = set.head;

while (current != NULL) {

if (current->data == element) {

return 1;

}

current = current->next;

}

return 0;

}

// Function to add an element to the set

void addElement(Set* set, int element) {

if (contains(*set, element)) {

printf("Element already exists in the set.\n");

return;

}

Node* newNode = (Node*)malloc(sizeof(Node));

if (newNode == NULL) {

printf("Memory allocation failed.\n");

return;

}

newNode->data = element;

newNode->next = set->head;

set->head = newNode;

printf("Element added to the set.\n");

}

// Function to remove an element from the set

void removeElement(Set* set, int element) {

Node* current = set->head;

Node* prev = NULL;

while (current != NULL) {

if (current->data == element) {

if (prev == NULL) {

set->head = current->next;

} else {

prev->next = current->next;

}

free(current);

printf("Element removed from the set.\n");

return;

}

prev = current;

current = current->next;

}

printf("Element not found in the set.\n");

}

// Function to print the elements of the set

void printSet(Set set) {

if (isEmpty(set)) {

printf("Set is empty.\n");

return;

}

printf("Elements in the set: ");

Node* current = set.head;

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

}

printf("\n");

}

// Function to free the memory used by the set

void freeSet(Set* set) {

Node* current = set->head;

while (current != NULL) {

Node* temp = current;

current = current->next;

free(temp);

}

set->head = NULL;

printf("Set freed.\n");

}

int main() {

Set set = createSet();

addElement(&set, 10);

addElement(&set, 20);

addElement(&set, 30);

addElement(&set, 20); // Duplicate element

printSet(set);

removeElement(&set, 20);

removeElement(&set, 40); // Element not present

printSet(set);

freeSet(&set);

return 0;

}


Output :

Element added to the set.

Element added to the set.

Element added to the set.

Element already exists in the set.

Elements in the set: 30 20 10

Element removed from the set.

Element not found in the set.

Elements in the set: 30 10

Set freed.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.