Challenge

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

PROJECT 1

/* - This header for standard input and output, functions for input and output operations. */

/* - this header provides functions n macros for operations, such as memory allocation and deallocation. */

#include   

#include

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

struct Node

{

    int data;

    struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data)

{

    // Allocate memory for a new node

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

    if (newNode == NULL)

{

        printf("Memory allocation not successful!\n");

        exit(1); // Exit the program if memory allocation fails

    }

    newNode->data = data;

    newNode->next = NULL;

    return newNode;

}

// Function to add an element to a set

void addToSet(struct Node** head, int data)

{

    struct Node* newNode = createNode(data);

    if (*head == NULL)

{

        // If the set is empty, set the new node as the head

        *head = newNode;

    }

else

{

        // Traverse and arrange the set to find the last node and link the new node

        struct Node* current = *head;

        while (current->next != NULL)

        {

         current = current->next;

        }

        current->next = newNode;

    }

}

// Function to print the elements of a Set

void printSet(struct Node* head)

{

    struct Node* current = head;

    while (current != NULL)

{

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

        current = current->next;

    }

    printf("\n");

}

// Function to perform the union of two sets

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

{

    struct Node* result = NULL;

    struct Node* current1 = set1;

    struct Node* current2 = set2;

    // Add elements from set1 to the result set

    while (current1 != NULL)

{

        addToSet(&result, current1->data);

        current1 = current1->next;

    }

    // Add elements from set2 to the result set (if they are not already present)

    while (current2 != NULL)

{

   //The !isInSet() function is used to check if an element is not already present in a set    

      if (!isInSet(result, current2->data)) 

{

            addToSet(&result, current2->data);

        }

        current2 = current2->next;

    }

    return result;

}

// Function to check if an element is in the set

int isInSet(struct Node* head, int data)

{

    struct Node* current = head;

    while (current != NULL)

    {

        if (current->data == data)

        {

            return 1; // Element found in the set

        }

        current = current->next;

    }

    return 0; // Element not found in the set

}

// Function to perform the intersection of two sets

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

{

    struct Node* result = NULL;

    struct Node* current1 = set1;

    // Add elements from set1 to the result set if they are also present in set2

    while (current1 != NULL)

    {

        if (isInSet(set2, current1->data))

        {

            addToSet(&result, current1->data);

        }

        current1 = current1->next;

    }

     return result;

}

int main()

{

    struct Node* set1 = NULL;

    struct Node* set2 = NULL;

    // Input for set1

    int n, data;

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

    scanf("%d", &n);

    printf("Enter the elements of set1: ");

    for (int i = 0; i < n>

      {

        scanf("%d", &data);

        addToSet(&set1, data);

      }

    // Input for set2

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

    scanf("%d", &n);

    printf("Enter the elements of set2: ");

    for (int i = 0; i < n>

    {

        scanf("%d", &data);

        addToSet(&set2, data);

    }

  //printSet function is used to display the elements of a set (implemented as a linked list) 

    printf("Set 1: ");

    printSet(set1);

    printf("Set 2: ");

    printSet(set2); 

   //declaring and initializing a pointer to a structure of type Node named unionSet

    struct Node* unionSet = setUnion(set1, set2);

    printf("Union: ");

    printSet(unionSet);

//declaring and initializing a pointer to a structure of type Node named intersectionSet

    struct Node* intersectionSet = setIntersection(set1, set2);

    printf("Intersection: ");

    printSet(intersectionSet);

    /* Free memory to release the dynamically allocated memory

   used by the result sets (unionSet and intersectionSet) */

    free(unionSet);

    free(intersectionSet);

    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.