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) (2 pts) Builds a simple linked

ID: 3817653 • Letter: W

Question

Write a program in C that does the following:


a) (2 pts) Builds a simple linked list consisting of 100 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) (1 pt.) Include a programmer-defined function in the same program as item 1 above 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) (1 pt.) Include a programmer-defined function in the same program as item 1 above 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) (3 pts.) Include a programmer-defined function in the same program as item 1 above called
reverse(arg) 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 print out its contents now that it is reversed order. This does NOT mean
that it will be read backwards, but rather, the now-reversed list will be read from head to
tail!


e) (2 pts.) Write a programmer-defined function called search(arg) that asks the user
what number (between 50 and 99) she wishes to find in the linked list. The function
searches for that number in the linked list and if found, prints out the position of the node
in which it found the number requested. If the number requested is not within the proper
range (50 to 99), then say so and give the user a second chance to enter it. If it is within the
range, but it is not found, it should say that the search could not find that number in the list.
Set up the user entry in a loop so that the user can enter numbers repeatedly if she wants.
That is, when the number is found (or not found), it will give her an opportunity to enter a
new number. If the user no longer wants to use the program, she should enter -1.
For output, print out a line stating that the value sought was found in the nth node. For
example:

Please enter the number to be sought in the list:
>> 15
The number 15 was found in node #78


Note: The program submitted shall have all those functions described above and will execute
them. The reason they are listed separately is that credit will be distributed among these
functions.

Explanation / Answer

Have written the code neatly with comments. Create a new file called a.c .

Copy the code into it and open with a code editor for best view.

Code is self explanatory. Thank you.

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

struct Node{ /* node of a link list*/
  
   struct Node *next;    /* Pointer to the next node*/
   int val;       /* value of the node*/
};

int getRandomNumber(){   /* returns random number between 50 to 100 inclusive*/
  
   int number=rand()%51;    /* generate a number between 0 to 50 inclusive */
   number=number+50;        /* add 50 to get a number between 50 to 100 inclusive*/
  
   return number;

}

/* b part of question printout function*/
void printout(struct Node *head){

   int i=0;   /* initialize node number */

   while(head!=NULL){   /* print till end of program */

       printf("Node #%d contains %d ",i,head->val );

       head=head->next;   /* traverse to next node */
       i=i+1;
   }
   printf(" ");
}

/* c part of question printout sum of all values in list*/
void sum(struct Node *head){

   int sum=0;   /* initialize sum*/

   while(head!=NULL){   /* print till end of program */

       sum=sum+head->val;   /* add sum */

       head=head->next;   /* traverse to next node */
   }

   printf("The sum total of all nodes in this list is %d ",sum);
   printf(" ");
}

/* d part of question reverse the list */
void reverse(struct Node **headAddress){

   struct Node *current=*headAddress;   /* get pointer to current head */

   struct Node *next_node=NULL;        /* initialize pointer to next node as null*/
   if(current){   /* list is not empty */

       next_node=current->next;   /* point pointer towards next node */
       current->next=NULL;       /* point current head to null */

   }
   while(next_node){   /* traverse till reached end node */

       struct Node *next_to_next=next_node->next;
       next_node->next=current;   /* reverse pointer of next node to current node */

       current=next_node;   /* make next node as current node for next iteration */
       next_node=next_to_next;    /* make next to next node as current node for next iteration */
   }

   *headAddress=current;    /* change to current head */


}

/* e part search answers in list */
void search(struct Node *head){

   struct Node *current=head;    /* initialize current pointer with head */

   int input=0; /* Initialize input to 0 */

  

   while(input!=-1){ /* keep running forever */

       printf("Please enter the number to be sought in the list: "); /* ask for input from user */
       scanf("%d",&input);   /* scan input */

       if(input==-1){
           break; /* break out of the loop */
       }else if(input<50 || input>100){ /* check for correct input range*/
           printf("Input not between 50-100 (inclusive) try again ");
       }else{

           current=head;   /* initialize current to head */
           int i=0;       /* initialize node number to i */
          
           while(current!=NULL && current->val!=input){   /* keep searching till a match is found or end of list is reached*/

               current=current->next;
               i=i+1;
           }
           if(current!=NULL){   /* match found */
               printf("The number %d was found in node #%d ",input,i);
           }else{ /* match not found */
               printf("search could not find %d in list ",input);
           }
       }

       printf(" ");
   }

}

int main(){

   srand(time(0));   /* seed random number */
   struct Node *head=NULL;   /* initialize head of list to null */
  
   for(int i=0;i<100;i++){ /* part 1 build a list of 100 nodes */

       struct Node *new_node=(struct Node *)malloc(sizeof(struct Node));   /* create a new node*/

       new_node->val=getRandomNumber(); /* assign a random number between 50 to 100 */
       new_node->next=NULL;               /* initialize next pointer to null */

       if(head==NULL){ /* currently zero nodes in the list */

           head=new_node;
       }else{  

           new_node->next=head;   /* add to front of the list */
           head=new_node;
       }

   }
   printout(head);
   sum(head);
   reverse(&head);
   printout(head);
   search(head);

}

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