CJCoding With Joseph
15free / daySign inCreate account

NumberChain: A Linked List Whose Destructor Deletes Every Node

Write a singly linked list class NumberChain that owns its nodes and deletes every one of them. Do NOT write a main function; a hidden test provides one.

Put a private struct Node { int value; Node* next; }; inside the class, and keep a Node* head plus an int count.

Required members:
- NumberChain() - starts empty: head is nullptr and count is 0.
- void pushBack(int value) - allocates one new Node and links it at the END.
- bool removeFirst() - deletes the FIRST node and returns true; on an empty chain it changes nothing and returns false.
- void clear() - deletes every remaining node and leaves the chain empty AND reusable.
- ~NumberChain() - must leave nothing allocated.
- int size() const - the number of nodes.
- int at(int index) const - the value at that position, counting from 0.

Free the memory you allocate - this question checks for memory leaks.

The hidden test reads n numbers and pushes them all, prints the size and then the values on one line, removes the first node and prints the new size and front value, clears the chain and prints the size, calls removeFirst() on the empty chain, then pushes 99 and prints the size and front value.

For the input:
4
5 10 15 20

your class must produce exactly the output shown in Expected Output.

Expected Output:

4
5 10 15 20
removed
3 10
0
empty
1 99

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
4\n5 10 15 20\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.