Challenge

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

Overview of the Project

The project focuses on implementing a set theory inteface using c programming language. User can input to data lists and the program computes the outputs, union and intersection of the elements of two lists.

Goals

  1. Implemet a set theory interface in C
  2. Accept user input for two lists and provide the union and intersection as output

Key Higlights of the Project

  1. Create link list using DMA
  2. user friendly input mechanism for specifying list element
  3. Efficient computation of intersection and union sets
  4. Proper memory management to prevent memory leaks

Deliverables of this Project

  1. Program that computes and display the intersection of two lists
  2. Program that computes and disply th union of two lists

Expected outcome

  1. Understanding of linked list implementation in C
  2. Proficiency in handling dynamic memory allocation
  3. Practical experience in set theory operations like union and intersection
  4. Appreciation of proper memory management practices in C programming

Program 

#include

#include

struct Node {

    int data;

    struct Node* next;

};

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;

    } else {

        struct Node* temp = *head;

        while (temp->next != NULL) {

            temp = temp->next;

        }

        temp->next = newNode;

    }

}

void printList(struct Node* head) {

    struct Node* temp = head;

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("\n");

}

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

    struct Node* result = NULL;

    while (list1 != NULL && list2 != NULL) {

        if (list1->data == list2->data) {

            insertNode(&result, list1->data);

            list1 = list1->next;

            list2 = list2->next;

        } else if (list1->data < list2>data) {

            list1 = list1->next;

        } else {

            list2 = list2->next;

        }

    }

    return result;

}

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

    struct Node* result = NULL;

    while (list1 != NULL) {

        insertNode(&result, list1->data);

        list1 = list1->next;

    }

    while (list2 != NULL) {

        insertNode(&result, list2->data);

        list2 = list2->next;

    }

    return result;

}

void freeList(struct Node* head) {

    struct Node* current = head;

    struct Node* next;

    while (current != NULL) {

        next = current->next;

        free(current);

        current = next;

    }

}

int main() {

    struct Node* list1 = NULL;

    struct Node* list2 = NULL;

    int n1, n2;

    // Input for List 1

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

    scanf("%d", &n1);

    printf("Enter �lements for List 1: ", n1);

    for (int i = 0; i < n1>

        int element;

        scanf("%d", &element);

        insertNode(&list1, element);

    }

    // Input for List 2

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

    scanf("%d", &n2);

    printf("Enter �lements for List 2: ", n2);

    for (int i = 0; i < n2>

        int element;

        scanf("%d", &element);

        insertNode(&list2, element);

    }

    // Print the original lists

    printf("\nList 1: ");

    printList(list1);

    printf("List 2: ");

    printList(list2);

    // Compute and print the intersection

    printf("\nIntersection: ");

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

    printList(intersectionResult);

    // Compute and print the union

    printf("Union: ");

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

    printList(unionResult);

    // Free allocated memory

    freeList(list1);

    freeList(list2);

    freeList(intersectionResult);

    freeList(unionResult);

    return 0;

}

 Expecting Output form the above program

[?2004l

Enter the number of elements in List 1: 4

Enter 4 elements for List 1: 2 4 6 8

Enter the number of elements in List 2: 3

Enter 3 elements for List 2: 4 8 10

List 1: 2 4 6 8 

List 2: 4 8 10 

Intersection: 4 8 

Union: 2 4 6 8 4 8 10 

Screenshot of program execution

Result

Interface for mathematical set theory using C programming lanuage was implemented,executed and tested.

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.