Write a program in C that does the following a Builds a simp

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!

** PLEASE MAKE SURE THIS PROGRAM WORKS IN C LANGUAGE !!

Solution

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

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

/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
   struct node* prev = NULL;
   struct node* current = *head_ref;
   struct node* next;
   while (current != NULL)
   {
       next = current->next;
       current->next = prev;
       prev = current;
       current = next;
   }
   *head_ref = prev;
}

/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
   /* allocate node */
   struct node* new_node =
           (struct node*) malloc(sizeof(struct node));
          
   /* put in the data */
   new_node->data = new_data;
              
   /* link the old list off the new node */
   new_node->next = (*head_ref);
      
   /* move the head to point to the new node */
   (*head_ref) = new_node;
}

/* Function to print linked list */
void printList(struct node *head)
{
   struct node *temp = head;
   int i=0;
   while(temp != NULL)
   {
       printf(\"Node #%d contains %d\ \",i, temp->data);
       temp = temp->next;
       i++;
   }
}
void sum(struct node *head)
{
int sum=0;
   struct node *temp = head;
   while(temp != NULL)
   {
       //printf(\"%d \", temp->data);
       sum+=temp->data;
       temp = temp->next;
   }
   printf(\"\ The sum total of all nodes in this list is %d\ \",sum);
}

/* Driver program to test above function*/
int main()
{
   /* Start with the empty list */
   struct node* head = NULL;
int i,t;
  
for(i=0;i<10;i++)
{
// (rand() % 51) generates random number between 0 and 50 adding 50 generates random number beween 50 and 100 both inclusive
t=(rand() % 51)+50;
push(&head, t);
}
     
  
   printf(\"Given linked list\ \");
   printList(head);
   sum(head);
   reverse(&head);                     
   printf(\"\ Reversed Linked list \ \");
   printList(head);
  
   return 0;
}

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 i
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 i
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 i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site