Please help to debug my code(need to hand-in by tmr) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please help to debug my code(need to hand-in by tmr)

https://code.sololearn.com/cA10A25A4A3a The code is a singly linked list which is a type of data structure that is made up of nodes that are created using self referential structures. Each of these nodes contains two parts, namely the data and the reference to the next list node. Only the reference to the first list node is required to access the whole linked list. This is known as the head. The last node in the list points to nothing so it stores NULL. Make options to let user choose the functions, including insertion, deletion, traverse and display. In insertion function, user needs to input the value and the location. My code can execute normally with less than three nodes. But problems happen when there is three nodes. Please help, this is my homework for tmr.

29th Dec 2020, 7:37 PM
John
John - avatar
4 Answers
+ 3
Thanks. To learn more about dynamic allocation in C++, you should look it up and view some tutorials, e.g. https://www.sololearn.com/learn/CPlusPlus/1632/ https://www.bogotobogo.com/cplusplus/memoryallocation.php The gist is that you allocate nodes with the new operator and use pointers to store their whereabouts: Node* node = new Node; Only when you no longer need the node, you deallocate it with the delete operator: delete node; Here is how you would apply it practically: https://code.sololearn.com/coV87nLV9yn2/?ref=app I only added the dynamic memory management and some default values for the variables. If there are still issues, please let me know. Nonetheless, it would be good to account for invalid position indices. The code could also benefit from refactoring it into smaller functions to make main() more readable.
29th Dec 2020, 8:36 PM
Shadow
Shadow - avatar
+ 2
Same answer as earlier: When inserting nodes, you need to allocate them dynamically, otherwise the node object is automatically destroyed once it goes out of scope, i.e. right after the switch statement, which leaves you with undefined pointers. Don't forget to delete the nodes later on. Furthermore, the loop condition during deletion should probably be ...; i < c; ... instead of ...; i < 2; ... Last but not least, it might be a good idea to account for the fact that indices greater than the list size can be entered in the according switch cases. The thread is a duplicate to these: [EDIT] Duplicate threads deleted. Please don't open multiple posts for the same issue. If necessary, you can edit your question. I would like to ask you to decide on one thread to keep, and delete the others.
29th Dec 2020, 8:02 PM
Shadow
Shadow - avatar
0
Sorry about the duplicate threads issue earlier, I had deleted the other two. Please explain in advance how do you allocate nodes dynamically.
29th Dec 2020, 8:07 PM
John
John - avatar
0
Thanks Shadow. I got it!
29th Dec 2020, 10:14 PM
John
John - avatar