Write a program in C that does the following: a) (2 pts) Builds a simple linked
ID: 3817704 • 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
please refer below code
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
typedef struct node
{
int info;
struct node *next;
}node;
void print_list(node *head)
{
node *travel = head;
int i=0;
while(travel != NULL)
{
printf("Node #%d contains %d ", i, travel->info);
i++;
travel = travel->next;
}
}
void sum_list(node *head)
{
node *travel = head;
int sum=0;
while(travel != NULL)
{
sum += travel->info;
travel = travel->next;
}
printf(" The total sum of all nodes in this list is %d ", sum);
}
void reverse_list(node **head)
{
node* prev = NULL;
node* current = *head;
node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
void search_list(int val,node *head)
{
int flag = 0,pos_node,i=0;
node *travel = head;
while(travel != NULL)
{
if(val == travel->info)
{
flag = 1;
pos_node = i;
break;
}
i++;
travel = travel->next;
}
if(flag)
{
printf(" The number %d was found in node #%d ", val,pos_node);
}
else
{
printf(" %d not found in list ", val);
}
}
int main()
{
node *head = NULL;
node *temp,*travel;
int i = 0,val;
srand(time(NULL));
for(i = 0; i < 100; i++)
{
val = 50 + (rand() % 51); //generating random number between 50 and 100
temp = (node *)malloc(sizeof(node)); //creating node
temp->info = val;
temp->next = NULL;
if(head == NULL) //if node to be inserted in linked list is first node
head = temp;
else //other than first node
{
travel = head;
while(travel->next != NULL)
travel = travel->next;
travel->next = temp;
}
}
printf("Original Linked List is ");
//printing linked list
print_list(head);
//sum of all info of nodes
sum_list(head);
printf(" Reversed Linked List is ");
//reversing linked list
reverse_list(&head);
print_list(head);
//search in list
do
{
while(1)
{
printf(" Please enter the number to be sought in the list: ");
scanf("%d", &val);
if(val >=50 && val <=100)
break;
else
continue;
}
search_list(val,head);
printf("Enter -1 for stop else enter any other number: ");
scanf("%d", &i);
}while(i != -1);
return 0;
}
please refer below output
Original Linked List is
Node #0 contains 73
Node #1 contains 67
Node #2 contains 96
Node #3 contains 53
Node #4 contains 83
Node #5 contains 97
Node #6 contains 74
Node #7 contains 91
Node #8 contains 91
Node #9 contains 97
Node #10 contains 92
Node #11 contains 62
Node #12 contains 60
Node #13 contains 84
Node #14 contains 68
Node #15 contains 65
Node #16 contains 86
Node #17 contains 52
Node #18 contains 73
Node #19 contains 50
Node #20 contains 88
Node #21 contains 74
Node #22 contains 87
Node #23 contains 97
Node #24 contains 89
Node #25 contains 85
Node #26 contains 89
Node #27 contains 95
Node #28 contains 79
Node #29 contains 86
Node #30 contains 90
Node #31 contains 61
Node #32 contains 97
Node #33 contains 61
Node #34 contains 92
Node #35 contains 76
Node #36 contains 84
Node #37 contains 52
Node #38 contains 94
Node #39 contains 82
Node #40 contains 79
Node #41 contains 95
Node #42 contains 74
Node #43 contains 99
Node #44 contains 71
Node #45 contains 54
Node #46 contains 100
Node #47 contains 65
Node #48 contains 75
Node #49 contains 79
Node #50 contains 88
Node #51 contains 50
Node #52 contains 63
Node #53 contains 82
Node #54 contains 64
Node #55 contains 50
Node #56 contains 57
Node #57 contains 76
Node #58 contains 84
Node #59 contains 89
Node #60 contains 58
Node #61 contains 73
Node #62 contains 50
Node #63 contains 59
Node #64 contains 59
Node #65 contains 58
Node #66 contains 92
Node #67 contains 77
Node #68 contains 59
Node #69 contains 77
Node #70 contains 63
Node #71 contains 68
Node #72 contains 69
Node #73 contains 97
Node #74 contains 63
Node #75 contains 51
Node #76 contains 83
Node #77 contains 81
Node #78 contains 80
Node #79 contains 68
Node #80 contains 61
Node #81 contains 87
Node #82 contains 60
Node #83 contains 90
Node #84 contains 70
Node #85 contains 100
Node #86 contains 84
Node #87 contains 72
Node #88 contains 54
Node #89 contains 98
Node #90 contains 75
Node #91 contains 85
Node #92 contains 75
Node #93 contains 87
Node #94 contains 63
Node #95 contains 75
Node #96 contains 67
Node #97 contains 75
Node #98 contains 51
Node #99 contains 66
The total sum of all nodes in this list is 7546
Reversed Linked List is
Node #0 contains 66
Node #1 contains 51
Node #2 contains 75
Node #3 contains 67
Node #4 contains 75
Node #5 contains 63
Node #6 contains 87
Node #7 contains 75
Node #8 contains 85
Node #9 contains 75
Node #10 contains 98
Node #11 contains 54
Node #12 contains 72
Node #13 contains 84
Node #14 contains 100
Node #15 contains 70
Node #16 contains 90
Node #17 contains 60
Node #18 contains 87
Node #19 contains 61
Node #20 contains 68
Node #21 contains 80
Node #22 contains 81
Node #23 contains 83
Node #24 contains 51
Node #25 contains 63
Node #26 contains 97
Node #27 contains 69
Node #28 contains 68
Node #29 contains 63
Node #30 contains 77
Node #31 contains 59
Node #32 contains 77
Node #33 contains 92
Node #34 contains 58
Node #35 contains 59
Node #36 contains 59
Node #37 contains 50
Node #38 contains 73
Node #39 contains 58
Node #40 contains 89
Node #41 contains 84
Node #42 contains 76
Node #43 contains 57
Node #44 contains 50
Node #45 contains 64
Node #46 contains 82
Node #47 contains 63
Node #48 contains 50
Node #49 contains 88
Node #50 contains 79
Node #51 contains 75
Node #52 contains 65
Node #53 contains 100
Node #54 contains 54
Node #55 contains 71
Node #56 contains 99
Node #57 contains 74
Node #58 contains 95
Node #59 contains 79
Node #60 contains 82
Node #61 contains 94
Node #62 contains 52
Node #63 contains 84
Node #64 contains 76
Node #65 contains 92
Node #66 contains 61
Node #67 contains 97
Node #68 contains 61
Node #69 contains 90
Node #70 contains 86
Node #71 contains 79
Node #72 contains 95
Node #73 contains 89
Node #74 contains 85
Node #75 contains 89
Node #76 contains 97
Node #77 contains 87
Node #78 contains 74
Node #79 contains 88
Node #80 contains 50
Node #81 contains 73
Node #82 contains 52
Node #83 contains 86
Node #84 contains 65
Node #85 contains 68
Node #86 contains 84
Node #87 contains 60
Node #88 contains 62
Node #89 contains 92
Node #90 contains 97
Node #91 contains 91
Node #92 contains 91
Node #93 contains 74
Node #94 contains 97
Node #95 contains 83
Node #96 contains 53
Node #97 contains 96
Node #98 contains 67
Node #99 contains 73
Please enter the number to be sought in the list:
12
Please enter the number to be sought in the list:
78
78 not found in list
Enter -1 for stop else enter any other number: 67
Please enter the number to be sought in the list:
67
The number 67 was found in node #3
Enter -1 for stop else enter any other number: 1
Please enter the number to be sought in the list:
56
56 not found in list
Enter -1 for stop else enter any other number: -1
Process returned 0 (0x0) execution time : 47.111 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.