w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How does a linked list know when its element has been removed from the list and adjust?

The list does not need to know anything about the iterator or its content. When the iterator removes its current node from the list, the iterator is invalidated. Even iterators in most standard STL container behave that same way. If you look at std::list, for instance, its erase(iterator) method returns a new iterator that represents the next node in the list following the node that was removed. You can do the same in your own remove() method. Otherwise, remove() would have to update the iterator to point at a different node, but that tends to go against how iterators typically operate.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.