Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program (IN C LANGUAGE, **NOT C++) that builds a simple linked list cont

ID: 3690353 • Letter: W

Question

Write a program (IN C LANGUAGE, **NOT C++) that builds a simple linked list containing integers from 0 to 20. The program then prints out the contents of the linked list to the screen in ascending order. It also adds the contents of all the nodes in the list and prints out the sum total at the bottom. The output functionality must be in a separate programmerdefined function and it must receive a pointer that points to the head of the list.

The output should look as follows:

Node #0 contains 0

Node #1 contains 1

Node #2 contains 2 . . .

Node #20 contains 20

The sum total of all nodes in this list is <sum>

Explanation / Answer

//add the elements in the linkedlist

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

struct test_struct

{

    int val;

    struct test_struct *next;

};

struct test_struct *head = NULL;

struct test_struct *curr = NULL;

struct test_struct* create_list(int val)

{

    printf(" creating list with headnode as [%d] ",val);

    struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));

    if(NULL == ptr)

    {

        printf(" Node creation failed ");

        return NULL;

  }

    ptr->val = val;

    ptr->next = NULL;

    head = curr = ptr;

    return ptr;

}

struct test_struct* add_to_list(int val, bool add_to_end)

{

    if(NULL == head)

    {

        return (create_list(val));

    }

    if(add_to_end)

        printf(" Adding node to end of list with value [%d] ",val);

    else

        printf(" Adding node to beginning of list with value [%d] ",val);

    struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));

    if(NULL == ptr)

    {

        printf(" Node creation failed ");

        return NULL;

    }

    ptr->val = val;

    ptr->next = NULL;

    if(add_to_end)

    {

        curr->next = ptr;

        curr = ptr;

    }

    else

    {

        ptr->next = head;

        head = ptr;

    }

    return ptr;

}

void display_nodeValues(void)

{

    struct test_struct *ptr = head;

    printf(" -------Printing list Start------- ");

    while(ptr != NULL)

    {

        printf(" [%d] ",ptr->val);

        ptr = ptr->next;

    }

    printf(" -------Printing list End------- ");

    return;

}

//to find the sum of elements in a list

int sum(test_struct *s)

{

int sum=0;

test_struct *t;

for(t=s;t!=NULL;t=t->next)

sum+=t->val;

return sum;

}

int main(void)

{

    int i = 0, ret = 0;

    struct test_struct *ptr = NULL;

    display_nodeValues();

    for(i = 5; i<10; i++)

        add_to_list(i,true);

    display_nodeValues();

    for(i = 4; i>0; i--)

        add_to_list(i,false);

    display_nodeValues();

    sum(*ptr);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote