This is a common problem in many languages. If you have a linear data structure and you repeat it, something should keep track of where you are in the structure. It may be the current pointer or a pointer, but it is some kind of finger pointing to the "current location".
If you modify the list during iteration, this cursor is likely to be wrong.
A common problem is that you delete the item under the cursor, all slide down, the next iteration of the loop increases the cursor, and you accidentally missed the item.
Some data structure implementations offer the ability to delete items during an iteration, but most of them do not work.
Ned Batchelder Jul 27 '10 at 18:35 2010-07-27 18:35
source share