Can anyone help me out of this infinite loop?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
23rd Dec 2021, 12:59 AM
Aditya
Aditya - avatar
3 Answers
+ 3
An option would be to take note of the node you begin to access, then on each iteration, check if the address of the next node you are accessing is equivalent to that one you first accessed. Just piggybacking off Carrie's suggestion: In this case the unique ID is the content of the node pointer. void Node :: printlist(Node *node){ Node* begin = node; while(node != NULL){ cout<<node->data<<" <---> "; node = node->next; if (node == begin) break; } cout<<"END"<<endl; }
23rd Dec 2021, 2:13 AM
Fermi
Fermi - avatar
+ 1
You probably need a unique id of some sort to identify each node since it's circular list, meaning the last node connects to the first node and therefore no NULL node exists. You then can use check their unique id to stop the iteration.
23rd Dec 2021, 1:19 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Thanks a lot dear sire..... Now if any body can help me with addAfter(😭)
23rd Dec 2021, 9:30 AM
Aditya
Aditya - avatar