Three-Node Chain Built With malloc
mediumQuestion 466Build a three-node singly linked list whose nodes live on the HEAP, walk it once, then tear it down. The node type struct Node (with an int value and a struct Node *next) is given.
Read three integers from input. Allocate three nodes with malloc (one per value), link them first to last, and set the last node's next to NULL. Then traverse from the head and print two lines:
- line 1: the values joined by -> (space, arrow, space), with no trailing arrow
- line 2: Total: followed by the sum of the values
For the input:
4 9 2
the output is exactly what is shown in Expected Output.
Free the memory you allocate before the program ends — this question checks for memory leaks.
Expected output
4 -> 9 -> 2 Total: 15
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...