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

<!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 9]><xml> Normal 0 false false false EN-US X-NONE X-NONE </xml><![endif]--><!-- [if gte mso 9]><xml> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0in; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} </style> <![endif]-->

#include

#include

 

struct node// here we defined structure as node . It contains an integer value and a pointer to the next node.

{

    int value;

    struct node* next;

};

 

struct node* insert(struct node* headnode, int n);//it is function prototype that will used in program.

void display(struct node* head);//it is function prototype that will used in program.

int compare(struct node* headnode, int n);//it is function prototype that will used in program.

struct node* Union(struct node* head1, struct node* head2);//it is function prototype that will used in program.

struct node* intersection(struct node* head1, struct node* head2);//it is function prototype that will used in program.

 

 

 

    struct node* head1 = NULL;// here we declare pointer for the first linked list

    struct node* head2 = NULL;// here we declare pointer for the second linked list

    struct node* UnionResult = NULL;//here we declare to store the results of the union

    struct node* IntersectionResult = NULL;//here we declare to store the results of the intersection

int main()

{

 

    int total_first_node, total_second_node, first_list_value, second_list_value;// here we declares the variables for asking to the user about nodes and its values.

    int i, k;// these variables are declare for loop execution.

 

    printf("Enter the first list total nodes\n");

    fflush(stdout);

    scanf("%d", &total_first_node);

 

    printf("Enter the second list total nodes\n");

    fflush(stdout);

    scanf("%d", &total_second_node);

 

    for (i = 1; i <= total_first_node; i++)

    {

        printf("Enter the %d values of first node \n", i);

        fflush(stdout);

        scanf("%d", &first_list_value);

        head1 = insert(head1, first_list_value);//We call the insert function to insert the value into the head1 linked list and update the head1 pointer.

        display(head1);//here We display the current state of head1 using the display function.

    }

 

    for (k = 1; k <= total_second_node; k++)

    {

        printf("Enter the %d values of second node \n", k);

        fflush(stdout);

        scanf("%d", &second_list_value);

        head2 = insert(head2, second_list_value);//We call the insert function to insert the value into the head2 linked list and update the head2 pointer.

        display(head2);//We display the current state of head2 using the display function.

    }

 

    printf("Union of two lists is:\n");

    UnionResult = Union(head1, head2);//We call the Union function, passing head1 and head2 as arguments and store the result in UnionResult.

    display(UnionResult);//We display the contents of UnionResult using the display function

 

    printf("Intersection of two lists is:\n");

    IntersectionResult = intersection(head1, head2);// similarly we do here as union.

    display(IntersectionResult);

 

    return 0;

}

 

struct node* insert(struct node* headnode, int n)// here we assign the insert function as structure pointer by giving one as integer variable and another as structure headnode pointer as an argument.

{

    struct node* newnode = (struct node*)malloc(sizeof(struct node));// here we are going to allocate memory for the newnode pointer then by using this newnode object we give the values for the structure node.

    newnode->value = n;

    newnode->next = headnode;

    return newnode;// here we used to return a pointer to the newly created node.

}

 

void display(struct node* head)//it is used for printing the elements of a linked list. It takes a pointer to the head of the linked list (head) as an argument.

{

    if (head == NULL)

    {

        printf("The list is empty\n");

    }

    else

    {

        struct node* tail = head;//Here, a new pointer tail is declared and initialized with the head pointer. This tail pointer will be used to traverse the linked list.

        while (tail != NULL)//This line starts a while loop that continues as long as tail is not NULL

        {

            printf("The value is %d\n", tail->value);

            tail = tail->next;

        }

    }

}

 

int compare(struct node* headnode, int n)//This line defines the compare function, which checks if a given value n exists in a linked list. It takes a pointer to the head of the linked list (headnode) and an integer value (n) as arguments.

{

    while (headnode != NULL)

    {

        if (headnode->value == n)

        {

            return 1;//Element found in the list

        }

        headnode = headnode->next;// it is used to give the next address for the given value.

    }

    return 0; // Element not found in the list

}

 

struct node* Union(struct node* head1, struct node* head2)//This line defines the Union function, which computes the union of two linked lists (head1 and head2).

{

    struct node* UnionResult = NULL;// here we assign the new pointer as  unionresult by which we can store the union values inside it. and by giving it as NULL we tell the program that the pointer doesn't contain a random memory address. It starts with a known value

 

    // Copy all elements from head1 to UnionResult

    while (head1 != NULL)

    {

        UnionResult = insert(UnionResult, head1->value);//here the program is copying all the values from head1 to unionresult.

        head1 = head1->next;

    }

 

  

    while (head2 != NULL) // Traverse head2 and insert elements not already in UnionResult

    {

        if (!compare(UnionResult, head2->value))// here we are going to compare that if the values of head1(i.e. stored inside the unionresult) is not same to the head2 elements.

        {

            UnionResult = insert(UnionResult, head2->value);// if the elements not same then here we again insert the head2 values inside the unionresult pointer.

        }

        head2 = head2->next;

    }

 

    return UnionResult;//which now points to the resulting linked list containing the union of the two input lists.

}

 

struct node* intersection(struct node* head1, struct node* head2)// similarly we did inside the intersection function but here we are going to store the values that are same as to head2 elements.

{

    struct node* IntersectionResult = NULL;

 

    while (head1 != NULL)

    {

        if (compare(head2, head1->value))

        {

            IntersectionResult = insert(IntersectionResult, head1->value);

        }

        head1 = head1->next;

    }

 

    return IntersectionResult;

}

 

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.