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

In C... Write a program that builds a simple linked list containing integers fro

ID: 3694068 • Letter: I

Question

In C...

Write a program 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 210.

I have this code but it is not working, how do i fix it?

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*head;


//append the values to the linked list.
void insrt(int nomm)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=nomm;
right=(struct node *)head;
while(right->next != NULL)
   right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}

//main function for the implementation.
int main()
{
//variable declaration.
int i,nomm;
//node structure.
struct node *n;
head=NULL;
//inserting for 20 values..
for(i=0;i<20;i++)
{
  
  
       printf("Enter the nommber to insrt : ");
           scanf("%d",&nomm);
           insrt(nomm);
      
}
return 0;
}

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;

//append the values to the linked list.
void insrt(int nomm)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=nomm;
if(head==NULL)
{
head=temp;
head->next=NULL;
return;
}
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
//main function for the implementation.
int main()
{
//variable declaration.
int i,nomm;
//node structure.
struct node *n;
head=NULL;
//inserting for 20 values..
for(i=0; i<=20; i++)
{
//printf("Enter the nommber to insert : ");
//scanf("%d",&nomm);
insrt(i);
}
n = head;
i=0;
int sum=0;
while(n!=NULL)
{
printf("Node #%d contains %d ",i,n->data);
i++;
sum += n->data;
n= n->next;
}
printf("The sum of all integers is %d ",sum);
return 0;
}