Challenge

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

struct Node {

    int data;

    struct Node *next;

};

struct Node *insertNode(struct Node *head, int data) {

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

    newNode->data = data;

    newNode->next = head;

    return newNode;

}

void displayList(struct Node *head) {

    struct Node *current = head;

    while (current != NULL) {

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

        current = current->next;

    }

    printf("\n");

}

struct Node *findIntersection(struct Node *list1, struct Node *list2) {

    struct Node *result = NULL;

    struct Node *current1 = list1;

    while (current1 != NULL) {

        struct Node *current2 = list2;

        while (current2 != NULL) {

            if (current1->data == current2->data) {

                result = insertNode(result, current1->data);

                break;

            }

            current2 = current2->next;

        }

        current1 = current1->next;

    }

    return result;

}

struct Node *findUnion(struct Node *list1, struct Node *list2) {

    struct Node *result = NULL;

    struct Node *current1 = list1;

    while (current1 != NULL) {

        result = insertNode(result, current1->data);

        current1 = current1->next;

    }

    struct Node *current2 = list2;

    while (current2 != NULL) {

        if (!containsElement(result, current2->data)) {

            result = insertNode(result, current2->data);

        }

        current2 = current2->next;

    }

    return result;

}

int main() {

    struct Node *list1 = NULL;

    struct Node *list2 = NULL;

    int n1, n2, data;

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

    scanf("%d", &n1);

    printf("Enter the elements of list 1:\n");

    for (int i = 0; i < n1>        scanf("%d", &data);

        list1 = insertNode(list1, data);

    }

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

    scanf("%d", &n2);

    printf("Enter the elements of list 2:\n");

    for (int i = 0; i < n2>        scanf("%d", &data);

        list2 = insertNode(list2, data);

    }

    printf("\nList 1: ");

    displayList(list1);

    printf("List 2: ");

    displayList(list2);

    struct Node *intersection = findIntersection(list1, list2);

    printf("\nIntersection of lists: ");

    displayList(intersection);

    struct Node *unionSet = findUnion(list1, list2);

    printf("Union of lists: ");

    displayList(unionSet);

}

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.