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

//for dynamic variables

#include

// Define a structure for a node in the linked list

struct Node

{

    int data;

    struct Node *next; // pointer pointing towards next node

};

// Initializing different lists that will be used in this program

struct Node *list1, *list2, *list_intersection, *list_union;

//Function to insert an elemenet into the linked list

struct Node *insert(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* current = head;

        while(current->next != NULL){

            current = current->next;

        }

        current->next = newNode;

    }

    return head;

}

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

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

    struct Node* current = head;

    while (current != NULL) {

        if (current->data == data) {

            return 1;

        }

        current = current->next;

    }

    return 0;

}

//Function to perform unionLists

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

    struct Node* result = NULL;

    struct Node* temp1 = list1;

    struct Node* temp2 = list2;

    while (temp1 != NULL) {

        if (!compare(result, temp1->data)) {

            result = insert(result, temp1->data);

        }

        temp1 = temp1->next;

    }

    while (temp2 != NULL) {

        if (!compare(result, temp2->data)) {

            result = insert(result, temp2->data);

        }

        temp2 = temp2->next;

    }

    return result;

}

// Function to perform the intersection of two linked lists

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

    struct Node* result = NULL;

    struct Node* temp1 = list1;

    while (temp1 != NULL) {

        if (compare(list2, temp1->data)) {

            result = insert(result, temp1->data);

        }

        temp1 = temp1->next;

    }

    return result;

}

// Function to print the linked list

void display_List(struct Node* head) {

    struct Node* current = head;

    while (current != NULL) {

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

        current = current->next;

    }

    printf("NULL\n");

}

int main()

{

    struct Node* list1 = NULL;

    struct Node* list2 = NULL;

    int n, m;

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

    scanf("%d", &n);

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

    for (int i = 0; i < n>

        int data;

        scanf("%d", &data);

        list1 = insert(list1, data);

    }

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

    scanf("%d", &m);

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

    for (int i = 0; i < m>

        int data;

        scanf("%d", &data);

        list2 = insert(list2, data);

    }

    printf("First List: ");

    display_List(list1);

    printf("Second List: ");

    display_List(list2);

    struct Node *list_union  = unionLists(list1, list2);

    printf("Union: ");

    display_List(list_union);

    struct Node *list_intersection = intersectionLists(list1, list2);

    printf("Intersection: ");

    display_List(list_intersection);

    return 0;

}

Input & Output:

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.