The following is such a trivial question that it probably has been asked before, however, I wasn't able to find an answer through search.
What I am trying to do is use std::forward_list to implement some of the exercises in "Cracking the Coding Interview" in C++. In my understanding, a linked list element contains a) data and b) a pointer to the next element. Some of the solutions require re-pointing the "next" pointer to another element, like this (Pseudo-Code)
node1.next = node1.next.next.next; // Effectively deletes 2 elements
However, I can't find any way to do this in the forward_list documentation? Am I missing something, or is it simply not possible?
I am perfectly happy if someone could point me to the correct question, thanks!
You can not do manual repointing in std::forward_list
, because it hides all mechanics from you. This is why it was created!
Instead, you need to perform business operations on it. For example, to delete elements from the list, you need to call erase_after()
- it even has the overload to delete two consequtive nodes in one shot.