← Back to Pointers
Question 466
Three-Node Chain Built With malloc
Three-Node Chain Built With malloc
Build a three-node singly linked list whose nodes live on the HEAP, walk it once, then tear it down. The node typestruct Node(with anint valueand astruct Node *next) is given. Read three integers from input. Allocate three nodes withmalloc(one per value), link them first to last, and set the last node'snexttoNULL. 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 — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.