0

Why this code shows "segmentation fault error"? In "Ascending ordered singly linked list" code

Note: <---Enter "total no of node value" which is greater than 2. Because this code show error,only when "total no of node value" greater than 2---> I also found that "ascend()" function is the reason for this error.. But I don't knows, what's gone wrong in "ascend()" function Please help to find the mistake done by me on "ascend() function" https://code.sololearn.com/cSS1XcRULCKL/?ref=app

4th Sep 2021, 3:45 PM
Yogeshwaran P
Yogeshwaran P - avatar
1 Answer
+ 1
Hi Yogeshwaran, I have found your mistake, you have to update the next, otherwise will access to some memory that it is pointed to NULL causing the error. Lets make an example to understand: Linked list 8->7->1 You have to check the first element with all the others and swap when the condition is verified (You have done it correctly!). 8>7 yes swap, then 7<1 yes swap You have now 1->8->7. After the end of the second loop, next will point to NULL and you dont update it, this is causing the segmentation fault in the second run of the loop of the second loop because you ll try to access to next->data but next is equal to NULL. Seems to me that addr is not needed. You dont use it in the algorithm. I d remove that addr and put after temp=temp->link in the first loop This line: next=temp->link If you want you can also modify the output() because it prints -> at the end that isn't needed.
6th Sep 2021, 12:11 PM
Gigi