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: 3822044 • 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 #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:

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

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

/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void insert(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));

struct node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */
new_node->data = new_data;

/* 3. This new node is going to be the last node, so make next of
it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */
last->next = new_node;
return;
}

// This function prints contents of linked list starting from head
void printOut(struct node *node)
{
int i=0;
while (node != NULL)
{
printf(" Node #%d contains %d ",i, node->data);
i++;
node = node->next;
}
}

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;
}

void sum(struct node *node)
{
int sum = 0;
while (node != NULL)
{
sum += node->data;
node = node->next;
}
printf("The sum total of all nodes in this list is %d ",sum);
}

void search(struct node *node,int data)
{
bool found = false;
int i=0;
while (node != NULL)
{
if(node->data == data)
{
found = true;
break;
}
node = node->next;
i++;
}
if(found)
printf(" The number %d was found in node #%d ",data,i);
else
printf("Not found try again ");
}


int main()
{
struct node* head = NULL;
  
int i;
for(i=0;i<100;i++)
{
int randNum = rand()%(50 + 1) + 50;
insert(&head,randNum);
}

printf(" Created Linked list is: ");
printOut(head);
  
sum(head);
  
reverse(&head);
  
printf(" reverse list is :");
printOut(head);
  
int num;
printf(" Please enter the number to be sought in the list: ");
scanf(" %d",&num);
while(num!=-1)
{
if(num < 50 && num > 99)
printf("Please enter a valid number ");
else
search(head,num);
  
printf(" Please enter the number to be sought in the list: ");
scanf(" %d",&num);
}

return 0;
}

//outPut:


Created Linked list is: Node #0 contains 60
Node #1 contains 99
Node #2 contains 59
Node #3 contains 84
Node #4 contains 82
Node #5 contains 87
Node #6 contains 87
Node #7 contains 98
Node #8 contains 95
Node #9 contains 69
Node #10 contains 55
Node #11 contains 81
Node #12 contains 70
Node #13 contains 69
Node #14 contains 79
Node #15 contains 72
Node #16 contains 95
Node #17 contains 80
Node #18 contains 90
Node #19 contains 81
Node #20 contains 85
Node #21 contains 58
Node #22 contains 86
Node #23 contains 89
Node #24 contains 64
Node #25 contains 55
Node #26 contains 58
Node #27 contains 54
Node #28 contains 81
Node #29 contains 78
Node #30 contains 67
Node #31 contains 65
Node #32 contains 77
Node #33 contains 50
Node #34 contains 73
Node #35 contains 83
Node #36 contains 87
Node #37 contains 60
Node #38 contains 54
Node #39 contains 81
Node #40 contains 53
Node #41 contains 59
Node #42 contains 86
Node #43 contains 73
Node #44 contains 52
Node #45 contains 90
Node #46 contains 70
Node #47 contains 97
Node #48 contains 94
Node #49 contains 59
Node #50 contains 52
Node #51 contains 78
Node #52 contains 93
Node #53 contains 88
Node #54 contains 67
Node #55 contains 56
Node #56 contains 67
Node #57 contains 100
Node #58 contains 60
Node #59 contains 72
Node #60 contains 77
Node #61 contains 77
Node #62 contains 88
Node #63 contains 53
Node #64 contains 52
Node #65 contains 85
Node #66 contains 60
Node #67 contains 89
Node #68 contains 69
Node #69 contains 64
Node #70 contains 95
Node #71 contains 98
Node #72 contains 98
Node #73 contains 80
Node #74 contains 70
Node #75 contains 50
Node #76 contains 94
Node #77 contains 64
Node #78 contains 71
Node #79 contains 88
Node #80 contains 99
Node #81 contains 98
Node #82 contains 65
Node #83 contains 91
Node #84 contains 85
Node #85 contains 56
Node #86 contains 71
Node #87 contains 52
Node #88 contains 55
Node #89 contains 81
Node #90 contains 74
Node #91 contains 83
Node #92 contains 82
Node #93 contains 86
Node #94 contains 60
Node #95 contains 84
Node #96 contains 96
Node #97 contains 71
Node #98 contains 98
Node #99 contains 64
The sum total of all nodes in this list is 7536

reverse list is : Node #0 contains 64
Node #1 contains 98
Node #2 contains 71
Node #3 contains 96
Node #4 contains 84
Node #5 contains 60
Node #6 contains 86
Node #7 contains 82
Node #8 contains 83
Node #9 contains 74
Node #10 contains 81
Node #11 contains 55
Node #12 contains 52
Node #13 contains 71
Node #14 contains 56
Node #15 contains 85
Node #16 contains 91
Node #17 contains 65
Node #18 contains 98
Node #19 contains 99
Node #20 contains 88
Node #21 contains 71
Node #22 contains 64
Node #23 contains 94
Node #24 contains 50
Node #25 contains 70
Node #26 contains 80
Node #27 contains 98
Node #28 contains 98
Node #29 contains 95
Node #30 contains 64
Node #31 contains 69
Node #32 contains 89
Node #33 contains 60
Node #34 contains 85
Node #35 contains 52
Node #36 contains 53
Node #37 contains 88
Node #38 contains 77
Node #39 contains 77
Node #40 contains 72
Node #41 contains 60
Node #42 contains 100
Node #43 contains 67
Node #44 contains 56
Node #45 contains 67
Node #46 contains 88
Node #47 contains 93
Node #48 contains 78
Node #49 contains 52
Node #50 contains 59
Node #51 contains 94
Node #52 contains 97
Node #53 contains 70
Node #54 contains 90
Node #55 contains 52
Node #56 contains 73
Node #57 contains 86
Node #58 contains 59
Node #59 contains 53
Node #60 contains 81
Node #61 contains 54
Node #62 contains 60
Node #63 contains 87
Node #64 contains 83
Node #65 contains 73
Node #66 contains 50
Node #67 contains 77
Node #68 contains 65
Node #69 contains 67
Node #70 contains 78
Node #71 contains 81
Node #72 contains 54
Node #73 contains 58
Node #74 contains 55
Node #75 contains 64
Node #76 contains 89
Node #77 contains 86
Node #78 contains 58
Node #79 contains 85
Node #80 contains 81
Node #81 contains 90
Node #82 contains 80
Node #83 contains 95
Node #84 contains 72
Node #85 contains 79
Node #86 contains 69
Node #87 contains 70
Node #88 contains 81
Node #89 contains 55
Node #90 contains 69
Node #91 contains 95
Node #92 contains 98
Node #93 contains 87
Node #94 contains 87
Node #95 contains 82
Node #96 contains 84
Node #97 contains 59
Node #98 contains 99
Node #99 contains 60

Please enter the number to be sought in the list: 33
Not found try again

Please enter the number to be sought in the list: 44
Not found try again

Please enter the number to be sought in the list: 88
The number 88 was found in node #20

Please enter the number to be sought in the list: -1

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