Back to problems
COMP2521
Linked List: Build and Print
Linked List: build a list and print it
Read n integers, link them into a list in input order, then print from head to tail. The build part (appending at the tail) is provided; you complete the traversal print.
Complete printList: walk a moving pointer cur from head, printing value -> for each node until NULL, then a final X to mark the end.
Pitfall (straight from the bug lab): the loop condition must becur != NULL, notcur->next != NULL(which drops the last node and segfaults on an empty list).
Input
- Line 1: integer
n(may be 0). - Line 2:
nspace-separated integers.
Output
- One line:
v1 -> v2 -> … -> X(each value followed by->, ending inX). An empty list prints justX.
Example: input 3 / 10 20 30 -> 10 -> 20 -> 30 -> X.
Samples
stdin
3 10 20 30
expected
10 -> 20 -> 30 -> X
stdin
1 42
expected
42 -> X
c-dcc
14