Challenge

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

Program Code:

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

struct Node {

int data;

struct Node* next;

};

struct Set {

struct Node* head;

};

// Function to create a new node

struct Node* createNode(int data) {

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

if (newNode == NULL) {

printf("Memory allocation failed\n");

exit(EXIT_FAILURE);

}

newNode->data = data;

newNode->next = NULL;

return newNode;

}

// Function to create an empty set

struct Set* createSet() {

struct Set* newSet = (struct Set*)malloc(sizeof(struct Set));

if (newSet == NULL) {

printf("Memory allocation failed\n");

exit(EXIT_FAILURE);

}

newSet->head = NULL;

return newSet;

}

// Function to check if the set is empty

bool isEmpty(struct Set* set) {

return set->head == NULL;

}

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

bool contains(struct Set* set, int element) {

struct Node* current = set->head;

while (current != NULL) {

if (current->data == element) {

return true;

}

current = current->next;

}

return false;

}

// Function to add an element to the set

void addElement(struct Set* set, int element) {

if (!contains(set, element)) {

struct Node* newNode = createNode(element);

newNode->next = set->head;

set->head = newNode;

}

}

// Function to perform union of two sets

struct Set* unionSet(struct Set* set1, struct Set* set2) {

struct Set* resultSet = createSet();

struct Node* current = set1->head;

while (current != NULL) {

addElement(resultSet, current->data);

current = current->next;

}

current = set2->head;

while (current != NULL) {

addElement(resultSet, current->data);

current = current->next;

}

return resultSet;

}

// Function to perform intersection of two sets

struct Set* intersectionSet(struct Set* set1, struct Set* set2) {

struct Set* resultSet = createSet();

struct Node* current = set1->head;

while (current != NULL) {

if (contains(set2, current->data)) {

addElement(resultSet, current->data);

}

current = current->next;

}

return resultSet;

}

// Function to display the elements of a set

void display(struct Set* set) {

struct Node* current = set->head;

printf("Set: { ");

while (current != NULL) {

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

current = current->next;

}

printf("}\n");

}

// Function to free the memory allocated for the set

void freeSet(struct Set* set) {

struct Node* current = set->head;

while (current != NULL) {

struct Node* temp = current;

current = current->next;

free(temp);

}

free(set);

}

// Main function

int main() {

struct Set* set1 = createSet();

struct Set* set2 = createSet();

int element, size1, size2;

printf("Enter the number of elements in Set 1: ");

scanf("%d", &size1);

printf("Enter elements for Set 1:\n");

for (int i = 0; i < size1; ++i) {

scanf("%d", &element);

addElement(set1, element);

}

printf("Enter the number of elements in Set 2: ");

scanf("%d", &size2);

printf("Enter elements for Set 2:\n");

for (int i = 0; i < size2; ++i) {

scanf("%d", &element);

addElement(set2, element);

}

printf("Set 1: ");

display(set1);

printf("Set 2: ");

display(set2);

struct Set* unionResult = unionSet(set1, set2);

printf("Union of Set 1 and Set 2: ");

display(unionResult);

struct Set* intersectionResult = intersectionSet(set1, set2);

printf("Intersection of Set 1 and Set 2: ");

display(intersectionResult);

// Free allocated memory

freeSet(set1);

freeSet(set2);

freeSet(unionResult);

freeSet(intersectionResult);

return 0;

}

Output:

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.