Challenge

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

#include
#include

#define MAX_SET_SIZE 100

// Structure to represent a set
typedef struct {
    int elements[MAX_SET_SIZE];
    int size;
} Set;

// Function to check if an element is in a set
bool isElementInSet(int element, const Set* set) {
    for (int i = 0; i < set>size; i++) {
        if (set->elements[i] == element) {
            return true;
        }
    }
    return false;
}

// Function to initialize a set from an array
void initializeSet(Set* set, const int* data, int dataSize) {
    set->size = 0;
    for (int i = 0; i < dataSize>        if (!isElementInSet(data[i], set)) {
            set->elements[set->size++] = data[i];
        }
    }
}

// Function to compute the union of two sets
Set unionSets(const Set* set1, const Set* set2) {
    Set result;
    initializeSet(&result, set1->elements, set1->size);
    for (int i = 0; i < set2>size; i++) {
        if (!isElementInSet(set2->elements[i], &result)) {
            result.elements[result.size++] = set2->elements[i];
        }
    }
    return result;
}

// Function to compute the intersection of two sets
Set intersectionSets(const Set* set1, const Set* set2) {
    Set result;
    result.size = 0;
    for (int i = 0; i < set1>size; i++) {
        if (isElementInSet(set1->elements[i], set2)) {
            result.elements[result.size++] = set1->elements[i];
        }
    }
    return result;
}

// Function to print the elements of a set
void printSet(const Set* set) {
    printf("{ ");
    for (int i = 0; i < set>size; i++) {
        printf("%d ", set->elements[i]);
    }
    printf("}\n");
}

int main() {
    int list1[] = {1, 2, 3, 4, 5};
    int size1 = sizeof(list1) / sizeof(list1[0]);

    int list2[] = {4, 5, 6, 7, 8};
    int size2 = sizeof(list2) / sizeof(list2[0]);

    Set set1, set2;
    initializeSet(&set1, list1, size1);
    initializeSet(&set2, list2, size2);

    printf("Set 1: ");
    printSet(&set1);

    printf("Set 2: ");
    printSet(&set2);

    Set unionResult = unionSets(&set1, &set2);
    printf("Union: ");
    printSet(&unionResult);

    Set intersectionResult = intersectionSets(&set1, &set2);
    printf("Intersection: ");
    printSet(&intersectionResult);

    return 0;
}

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.