Challenge

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

c code

#include

#include

// Node structure for the linked list

typedef struct Node {

    int data;

    struct Node *next;

} Node;

// Function to insert an element into a linked list

void insert(Node **head, int data) {

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

    newNode->data = data;

    newNode->next = NULL;

    if (*head == NULL) {

        *head = newNode;

    } else {

        Node *temp = *head;

        while (temp->next != NULL) {

            temp = temp->next;

        }

        temp->next = newNode;

    }

}

// Function to print the elements of a linked list

void printList(Node *head) {

    while (head != NULL) {

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

        head = head->next;

    }

    printf("\n");

}

// Function to find the union of two sets

Node *setUnion(Node *set1, Node *set2) {

    Node *result = NULL;

    while (set1 != NULL) {

        insert(&result, set1->data);

        set1 = set1->next;

    }

    while (set2 != NULL) {

        if (!search(result, set2->data)) {

            insert(&result, set2->data);

        }

        set2 = set2->next;

    }

    return result;

}

// Function to find the intersection of two sets

Node *setIntersection(Node *set1, Node *set2) {

    Node *result = NULL;

    while (set1 != NULL) {

        if (search(set2, set1->data)) {

            insert(&result, set1->data);

        }

        set1 = set1->next;

    }

    return result;

}

// Function to check if an element is present in a linked list

int search(Node *head, int key) {

    while (head != NULL) {

        if (head->data == key) {

            return 1; // Element found

        }

        head = head->next;

    }

    return 0; // Element not found

}

// Function to free the memory allocated for the linked list

void freeList(Node *head) {

    Node *temp;

    while (head != NULL) {

        temp = head;

        head = head->next;

        free(temp);

    }

}

int main() {

    Node *set1 = NULL;

    Node *set2 = NULL;

    // Example input: insert elements into sets

    insert(&set1, 1);

    insert(&set1, 2);

    insert(&set1, 3);

    insert(&set2, 2);

    insert(&set2, 3);

    insert(&set2, 4);

    printf("Set 1: ");

    printList(set1);

    printf("Set 2: ");

    printList(set2);

    Node *unionResult = setUnion(set1, set2);

    printf("Union: ");

    printList(unionResult);

    Node *intersectionResult = setIntersection(set1, set2);

    printf("Intersection: ");

    printList(intersectionResult);

    // Free the memory allocated for the linked lists

    freeList(set1);

    freeList(set2);

    freeList(unionResult);

    freeList(intersectionResult);

    return 0;

}

output

Set 1: 1 2 3 

Set 2: 2 3 4 

Union: 1 2 3 4 

Intersection: 2 3

#include 
#include 

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

// Function to insert an element into a linked list
void insert(Node **head, int data) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
    } else {
        Node *temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

// Function to print the elements of a linked list
void printList(Node *head) {
    while (head != NULL) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

// Function to find the union of two sets
Node *setUnion(Node *set1, Node *set2) {
    Node *result = NULL;
    while (set1 != NULL) {
        insert(&result, set1->data);
        set1 = set1->next;
    }
    while (set2 != NULL) {
        if (!search(result, set2->data)) {
            insert(&result, set2->data);
        }
        set2 = set2->next;
    }
    return result;
}

// Function to find the intersection of two sets
Node *setIntersection(Node *set1, Node *set2) {
    Node *result = NULL;
    while (set1 != NULL) {
        if (search(set2, set1->data)) {
            insert(&result, set1->data);
        }
        set1 = set1->next;
    }
    return result;
}

// Function to check if an element is present in a linked list
int search(Node *head, int key) {
    while (head != NULL) {
        if (head->data == key) {
            return 1; // Element found
        }
        head = head->next;
    }
    return 0; // Element not found
}

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

int main() {
    Node *set1 = NULL;
    Node *set2 = NULL;

    // Example input: insert elements into sets
    insert(&set1, 1);
    insert(&set1, 2);
    insert(&set1, 3);

    insert(&set2, 2);
    insert(&set2, 3);
    insert(&set2, 4);

    printf("Set 1: ");
    printList(set1);

    printf("Set 2: ");
    printList(set2);

    Node *unionResult = setUnion(set1, set2);
    printf("Union: ");
    printList(unionResult);

    Node *intersectionResult = setIntersection(set1, set2);
    printf("Intersection: ");
    printList(intersectionResult);

    // Free the memory allocated for the linked lists
    freeList(set1);
    freeList(set2);
    freeList(unionResult);
    freeList(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.