Linked List
Implement a singly linked list from scratch. Create a Node class with value and next fields, and a LinkedList class with add, remove, and print operations.
Process commands from stdin.
Input
- Line 1: number of commands N (1 <= N <= 2·10^4)
- For each command:
- Line 1: command type (
add,remove, orprint) - Only for
addandremove, line 2: the integer X (anyintvalue)
- Line 1: command type (
Behavior:
add X: add integer X to the end of the listremove X: remove the first occurrence of X (printNot foundif X is not in the list)print: print all elements separated by->, orEmptyif the list is empty
Output
- For each
print: elements in order separated by->, orEmpty - For each failed
remove:Not found
Examples
Input:
6
add
10
add
20
add
30
print
remove
20
print
Output:
10 -> 20 -> 30
10 -> 30
Input:
4
add
5
remove
5
remove
5
print
Output:
Not found
Empty
Input:
5
add
1
add
2
add
3
remove
1
print
Output:
2 -> 3
Removing the head node means the list's head reference itself has to
move to the second node. There's no "previous" node to relink.