@LowestOne Yes. vlad is correct. The fact that you're doing a count on the fly is hugely inefficient and definitely not something that should be recommended. It's a complicated solution to a simple problem.
The fact that you're doing a count on the fly is hugely inefficient and definitely not something that should be recommended.
Not in the general case. For a list limited to 10 nodes.. maybe it's not so bad.
What if we kept a queue of unused nodes as part of the implementation and just depended on knowing if the queue was empty to stop insertions? Is keeping a count still the only way?
If you initialise a counter to 10 and subtract 1 when adding a node - that's a counter.
If you initialise a queue with 10 items and remove 1 when adding a node, that's also a counter.
If you initialise a counter to 10 and subtract 1 when adding a node - that's a counter.
Yes.
If you initialise a queue with 10 items and remove 1 when adding a node, that's also a counter.
No. The list can be completely oblivious of any count. There's not necessarily a hidden count either as queue implementations don't require one.
edit:
By way of illustration:
My nephew has this transparent tubing construct that he likes to run marbles through. He also has a jar of marbles that attaches to the end of the tube to catch the marbles when they've completed their journey.. The typical use case: He'll take marbles from the jar and insert them into the tube and watch them go down until they return to the jar.