Challenge

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

Solution :

//Preprocessor section/header files
#include //Standard input\output Library
#include //Standard Library

//Define a structure
struct Node
{
    int num; //Declare integer variable
    struct Node* nextptr; //Pointer to the next Node
};

//Function prototypes
struct Node* createNodeList(int num); //Create a node list
void insertNode(struct Node** tmp, int num); //Insert a node
void displayList(struct Node* tmp); //Display the elements of a list
struct Node* intersection(struct Node* list1, struct Node* list2); //Find the intersection of two lists
struct Node* unionSet(struct Node* list1, struct Node* list2); //Find the union of two lists
int search(struct Node* tmp, int num); //Search for an element in a list

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

    //Input for List 1
    int num1;
    printf("Enter the number of elements for List 1 : ");
    fflush(stdout);
    scanf("%d", #1);
    for (int i = 0; i < num1>    {
        int element;
        printf("Enter element %d : ", i);
        fflush(stdout);
        scanf("%d", &element);
        insertNode(&list1, element);
    }

    //Input for List 2
    int num2;
    printf("Enter the number of elements for List 2 : ");
    fflush(stdout);
    scanf("%d", #2);
    for (int i = 0; i < num2>    {
        int element;
        printf("Enter element %d : ", i);
        fflush(stdout);
        scanf("%d", &element);
        insertNode(&list2, element);
    }

    printf("List 1 : ");
    displayList(list1);
    printf("List 2 : ");
    displayList(list2);

    struct Node* intersectionResult = intersection(list1, list2); //Find the intersection of List 1 and List 2.
    printf("Intersection : "); //Print the intersection between list of elements
    displayList(intersectionResult); //display the intersection of List 1 and List 2

    struct Node* unionResult = unionSet(list1, list2); //Find the union of List 1 and List 2
    printf("Union : ");//Print the union of list of elements
    displayList(unionResult); //display the union of List 1 and List 2

    return 0;
}

//Function to create a node list
struct Node* createNodeList(int num)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->num = num;
    newNode->nextptr = NULL;
    return newNode;
}

//Function to insert a node
void insertNode(struct Node* *tmp, int num)
{
    struct Node* newNode = createNodeList(num);
    if (*tmp == NULL)
    {
        *tmp = newNode;
    }
    else
    {
        struct Node* current = *tmp;
        while (current->nextptr != NULL)
        {
            current = current->nextptr;
        }
        current->nextptr = newNode;
    }
}

//Function to display the elements of a list
void displayList(struct Node* tmp)
{
    struct Node* current = tmp;
    while (current != NULL)
    {
        printf("%d ", current->num);
        current = current->nextptr;
    }
    printf("\n");
}

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

    while (list1 != NULL && list2 != NULL)
    {
        if (list1->num == list2->num)
        {
            insertNode(&result, list1->num);
            list1 = list1->nextptr;
            list2 = list2->nextptr;
        }
        else if (list1->num < list2>num)
        {
            list1 = list1->nextptr;
        }
        else
        {
            list2 = list2->nextptr;
        }
    }
    return result;
}

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

    while (list1 != NULL)
    {
        insertNode(&result, list1->num);
        list1 = list1->nextptr;
    }

    while (list2 != NULL)
    {
        if (!search(result, list2->num))
        {
            insertNode(&result, list2->num);
        }
        list2 = list2->nextptr;
    }

    return result;
}

//Function to search for an element in a list
int search(struct Node* tmp, int num)
{
    struct Node* current = tmp;
    while (current != NULL)
    {
        if (current->num == num)
        {
            return 1; //Element found
        }
        current = current->nextptr;
    }
    return 0; //Element not found
}

Output :

Enter the number of elements for List 1 : 4
Enter element 0 : 1
Enter element 1 : 2
Enter element 2 : 3
Enter element 3 : 4
Enter the number of elements for List 2 : 4
Enter element 0 : 3
Enter element 1 : 4
Enter element 2 : 5
Enter element 3 : 6
List 1 : 1 2 3 4 
List 2 : 3 4 5 6 
Intersection : 3 4 
Union : 1 2 3 4 5 6

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.