Can we pass linked list inside function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we pass linked list inside function?

Why below code has not proper working of display function from compare function? https://code.sololearn.com/cPyG6dgpwd3W/?ref=app

6th Oct 2019, 2:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 2
In the display function, the code accesses the value using pHeadNode. So after a call to display, pHeadNode will point to null and the list is, therefore, lost in memory. Thus, successive calls to display do not work. The solution is to simply make a copy of pHeadNode and iterate over the list using the copy node. clsNode* cpy = pHeadNode; while(cpy!=NULL) { cout << cpy->intData; cout << endl; cpy = cpy->pNext; }
6th Oct 2019, 2:44 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar