Issues creating and iterating through Linked List?

后端 未结 5 1370
眼角桃花
眼角桃花 2021-01-15 20:10

When I run the program below, it prints \"one: 1\", instead of \"one : 1, two: 2\", as I had expected. Anyone know what\'s going on here? I am trying to create a function th

5条回答
  •  醉酒成梦
    2021-01-15 20:43

    Without seeing your iterate function the problem is that head1 is pointing to the last element on the list, not the first. You probably meant the the last line of the add function to be:

    return head;
    

    and 2nd line of your program to be:

    head1 = add(2, "two", head1);
    

    But that will still probably give you your output in the wrong order, because your add function is adding the new element to the start of the list, not the end.

提交回复
热议问题