I was wondering if it is possible to traverse a linked list like this:
currentNode = randomNode;//where randomNode may or may not = firstNode
prevNode = firstNode;
while(prevNode != currentNode && prevNode->link != currentNode)
{
prevNode = prevNode->link;
}
Is it possible to do this in C++ when I am trying to find the node before currentNode in a singly linked list?
I trying to implement something like this in a console app for a school assignment so assume that I can’t use anything fancy like the boost libraries/list/anything that makes life easier, etc. So basically, I have only fairly primitive datatypes and libraries at my disposal.
You might want to make sure that prevNode->link is not a null reference either, in case currentNode is not actually linked.