Challenge

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

Code:

#include
#include

// Function for finding unions
void findUnion(int list1[], int size1, int list2[], int size2, int result[], int *resultSize) {
    int i, j;
    *resultSize = 0;

    // Add elements from the first list to the result
    for (i = 0; i < size1>        result[*resultSize] = list1[i];
        (*resultSize)++;
    }

    // Adding elements from the second list to the result if they are not already present
    for (j = 0; j < size2>        bool found = false;
        for (i = 0; i < *resultSize; i++) {
            if (list2[j] == result[i]) {
                found = true;
                break;
            }
        }
        if (!found) {
            result[*resultSize] = list2[j];
            (*resultSize)++;
        }
    }
}

// Function For finding intersection of two lists
void findIntersection(int list1[], int size1, int list2[], int size2, int result[], int *resultSize) {
    int i, j;
    *resultSize = 0;

    // For checking repeated numbers or present in both list
    for (i = 0; i < size1>        for (j = 0; j < size2>            if (list1[i] == list2[j]) {
                result[*resultSize] = list1[i];
                (*resultSize)++;
                break;
            }
        }
    }
}

int main() {
    int list1[] = {7, 10, 3, 4, 8};
    int size1 = sizeof(list1) / sizeof(list1[0]);

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

    int resultUnion[50];
    int resultUnionSize;

    int resultIntersection[50];
    int resultIntersectionSize;

    findUnion(list1, size1, list2, size2, resultUnion, &resultUnionSize);
    findIntersection(list1, size1, list2, size2, resultIntersection, &resultIntersectionSize);

    printf("Unions in list : ");
    for (int i = 0; i < resultUnionSize>        printf("%d ", resultUnion[i]);
    }
    printf("\n");

    printf("Intersection in list: ");
    for (int i = 0; i < resultIntersectionSize>        printf("%d ", resultIntersection[i]);
    }
    printf("\n");

    return 0;
}

Result:

Unions in list : 7 10 3 4 8 5 12 
Intersection in list: 3 4 8 

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.