adjacency list cpp error,need help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

adjacency list cpp error,need help

i am having an infinite loop error on display_graph function, obviously code is not complete but could someone tell me what is the issue and how to fix it. here is the link: https://code.sololearn.com/cx92z1hHq8R6/#cpp

17th Jan 2019, 8:37 AM
Ash
5 Answers
+ 3
vertices[5] is allocated but not initialized. list constructor has 5 so initializes 0 to 4. add_vertex called twice will corrupt data as vertices[6] is accessed. Left code comment answer also.
18th Jan 2019, 5:11 PM
John Wells
John Wells - avatar
+ 2
You have two nodes each claiming the other is next so your list doesn't end.
18th Jan 2019, 3:59 PM
John Wells
John Wells - avatar
+ 2
depends on what you want. Before add_edge calls, vertices each have one link. After first call, vertices point to newnode1 or newnode2 which have null. After second call, you reuse newnode1 and newnode2 changing existing lists and possibly making different lists. If you enter different numbers for v1 & v2 the second time, things won't be as screwed up as using the same ones.
18th Jan 2019, 4:28 PM
John Wells
John Wells - avatar
0
Thank you for replying, I tried the logic of insertion in linked list and created two newnodes and set there next pointer to vertices array's next pointer and then replaced vertices array's next pointer to point to these newnodes Now if this is wrong how should I fix it
18th Jan 2019, 4:12 PM
Ash
0
instead of class variables when i put those pointers inside function it works fine except when i delete them. here is the new code anything else i must do to make it better: https://code.sololearn.com/c4H3U3x5u5QX/#cpp i understand a little of what you are trying to say.could you tell me the topics i might be lacking in so that i may cover them for better understanding of cpp. thank you for your help
18th Jan 2019, 4:59 PM
Ash