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

Write a program in C that does the following: a) Builds a simple linked list con

ID: 3775630 • Letter: W

Question

Write a program in C that does the following:
a) Builds a simple linked list consisting of 10 nodes each of which contains a random integer between 50 and 100 inclusive. The list is to be built in main() and must be a local variable in main().
b) Includes a user-defined function named printout(arg) that takes in the head pointer of the already-built linked list as an argument and prints out the contents of the linked list to the screen, starting from the head of the list to the tail. See the output below.
Node #0 contains 53 Node #1 contains 78 Node #2 contains 95 . . . Node #20 contains 100
c) Includes a user-defined function called sum() that adds up the contents of the data members of each node in the list, and prints out the total as follows: The sum total of all nodes in this list is <whatever the sum total is>
d) Includes a user-defined function called reverse() that takes in the head pointer of the already-built linked list as an argument and will physically reverse the order of the nodes in the list (i.e., the new head will be the old tail and the new tail will be the old head of the list). Use the function printout() to output its contents now in reverse order. This does NOT mean that it will be read backwards, but rather, the reversed list will be read from head to tail!
Building and Working with Simple Linked Lists Write a program in Cthat does the following: a) Builds a simple linked list consisting of 10 nodes each of which contains a random integer between 50 and 100 inclusive. The list is to be built in main and must be a local variable in main b) Includes a user-defined function named printout (arg) that takes in the head pointer of the already-built linked list as an argument and prints out the contents of the linked list to the screen, starting from the head of the list to the tail. See the output below. Node 0 contains 53 Node 1 contains 78 Node #2 contains 95 Node #20 contains 100 c) Includes a user-defined function called sum that adds up the contents of the data members of each node in the list, and prints out the total as follows: The sum total of all nodes in this list is

Explanation / Answer

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

/* Link list node */
struct node
{
   int data;
   struct node* next;
};

/* Reverse the linked list */
static void reverse(struct node** head)
{
   struct node* prev = NULL;
   struct node* present = *head;
   struct node* next;
   while (present != NULL)
   {
       next = present->next;
       present->next = prev;
       prev = present;
       present = next;
   }
   *head = prev;
}

/* Function to insert a node */
void insert(struct node** head, int new_data)
{
   /* allocate node */
   struct node* new_node =   (struct node*) malloc(sizeof(struct node));
          
   /* insert data */
   new_node->data = new_data;
              
   /* link nodes */
   new_node->next = (*head);
      
   /* point head to new node */
   (*head) = new_node;
}

/* Function to printout linked list */
void printout(struct node *head)
{
   struct node *temp = head;
   while(temp != NULL)
   {
       printf("%d ", temp->data);
       temp = temp->next;
   }
}

/* Function to sum linked list */
int sum(struct node *head)
{
int s = 0;
   struct node *temp = head;
   while(temp != NULL)
   {
       s = s + temp->data;
       temp = temp->next;
   }
   return s;
}

/* Main program to test above function*/
int main()
{
   /* Start with the empty list */
   struct node* head = NULL;
int i = 0,num;
while(i<10){
// Generate random number between 50 and 100 inclusive
num = rand() % (100 + 1 - 50) + 50;
insert(&head, num);
i++;
}
     
   printf("Given linked list ");
   printout(head);
   reverse(&head);      
   printf(" Sum of Linked List:%d ",sum(head));
   printf(" Reversed Linked list ");
   printout(head);
}

/* sample output

*/

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