+ 1

Linked list function not working

The function insertspecific() isn't working properly It just adds element to the end of the list. 1 3 1 1 3 4 4 1 2 2 8 This can be used as sample input And it should give 1 2 3 4 As output But instead it shows 1 3 4 2 https://code.sololearn.com/cMnezl6laCHW/?ref=app

20th Jan 2020, 5:05 PM
Yashu Garg
Yashu Garg - avatar
5 Respostas
+ 2
Hi Yashu Garg The problem is in how you are counting the positions in the list with k at the loop at line 61 and then how you search for such position in the loop at line 72. By the way, your code in insertspecifix does not allow a node to be inserted in the head position. How are you counting the positions? For example, in a list like this: [1|•]->[2|•]->[3|•]-# I would number the positions like: <p0>[1|•]<p1>[2|•]<p2>[3|•]<p3> How are you counting then?
20th Jan 2020, 5:23 PM
Mark
+ 1
Yashu Garg I edited a bit my comment above. And what meant about the loop at line 61 and 72 is that they are not aligned. In other words the second loop at line 72 is overcounting.
20th Jan 2020, 5:31 PM
Mark
+ 1
Yashu Garg you should not comment it. What I mean is that you should adjust the loop at line 72 to match the way you are counting positions. Notice for example you are using a <=.
20th Jan 2020, 5:35 PM
Mark
0
Yes i am counting the positions like this only <p0>[1|•]<p1>[2|•]<p2>[3|•]<p3> And for that loop (k), even if i comment it, it still doesn't work
20th Jan 2020, 5:31 PM
Yashu Garg
Yashu Garg - avatar
0
Oh okay Thankyou!
20th Jan 2020, 5:39 PM
Yashu Garg
Yashu Garg - avatar