Challenge

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

/*
 * Project1.c
 *
 *  Created on: Aug 2, 2023
 *      Author: Asus
 */
#include
#include

// Node structure for linked list
struct Node {
    int data;
    struct Node* next;
};

// Function to insert a node at the end of the linked list
void insertNode(struct Node** head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    struct Node* current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
}

// Function to print the linked list
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("(%p: %d) ", current, current->data); // Printing both address and data
        current = current->next;
    }
    printf("\n");
}


// Function to find the intersection of two linked lists
struct Node* findIntersection(struct Node* list1, struct Node* list2) {
    struct Node* result = NULL;
    struct Node* current = list1;

    while (current != NULL) {
        struct Node* temp = list2;
        while (temp != NULL) {
            if (current->data == temp->data) {
                insertNode(&result, current->data);
                break;
            }
            temp = temp->next;
        }
        current = current->next;
    }

    return result;
}

// Function to find the union of two linked lists
struct Node* findUnion(struct Node* list1, struct Node* list2) {
    struct Node* result = NULL;
    struct Node* current = list1;

    while (current != NULL) {
        insertNode(&result, current->data);
        current = current->next;
    }

    current = list2;
    while (current != NULL) {
        struct Node* temp = result;
        int found = 0;
        while (temp != NULL) {
            if (current->data == temp->data) {
                found = 1;
                break;
            }
            temp = temp->next;
        }
        if (!found) {
            insertNode(&result, current->data);
        }
        current = current->next;
    }

    return result;
}

// Function to free memory allocated for the linked list
void freeList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        struct Node* temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    struct Node* list1 = NULL;
    struct Node* list2 = NULL;

    // Sample data for the first list
    insertNode(&list1, 2);
    insertNode(&list1, 4);
    insertNode(&list1, 6);
    insertNode(&list1, 8);

    // Sample data for the second list
    insertNode(&list2, 3);
    insertNode(&list2, 6);
    insertNode(&list2, 9);

    printf("List 1: ");
    printList(list1);

    printf("List 2: ");
    printList(list2);

    struct Node* intersection = findIntersection(list1, list2);
    printf("Intersection: ");
    printList(intersection);

    struct Node* unionSet = findUnion(list1, list2);
    printf("Union: ");
    printList(unionSet);

    // Free allocated memory for the linked lists
    freeList(list1);
    freeList(list2);
    freeList(intersection);
    freeList(unionSet);

    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.