aspose file tools*
The moose likes Java in General and the fly likes Weird thing happening in Linked Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Weird thing happening in Linked Lists" Watch "Weird thing happening in Linked Lists" New topic
Author

Weird thing happening in Linked Lists

akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Hi all,
I have a linked list that is supposed to return a modified list..

IntNode removeAfterEvery(IntNode head, int oldvalue): If oldvalue is in the list, remove the value immediately after EVERY occurrence of oldvalue. Return the modified list. For example, if the list is currently
9 -> 4 -> 3 -> 12 -> 4 -> 1 -> 4

and we call removeAfterEvery(list, 4), the list should then become

9 -> 4 -> 12 -> 4 -> 4

I have written a code for this and it works.. Only with a problem that it modifies a variable which it is not supposed to and does not modify the one it is supposed to ..




Above is the code which calls the method.. When I try to print "modifiedList" , it prints an empty list whereas it should be the correct one.. and when I try print "head", it prints the correct list. But I never do anything with the original list..

Below is the method..




As you can see above, the method does not change anything in the head list. Whatever it changes, is done in pointer list, which is a copy of head. I am not returning head and not doing anything with head. but still it changes and displays the correct list..


Why is this happening?? I am pretty sure that there is a simple reason behind this but I can't understand what's the mystery.. PLease help!




Thanks!!






Manish Chhabra
Greenhorn

Joined: Feb 24, 2010
Posts: 15

Hi Akash,

Your method removeAfterEvery takes the reference of head (of linked list), any modifications to that will reflect in the main method.

Second thing you are returning "pointer" back from your removeAfterEvery method, which is null after your while loop while ( pointer != null) has finished execution. Ideally you should not be returning anything because it is pass by reference. Or use return "head" instead of "pointer" from your removeAfterEvery method which will give you modified list in modifiedList.

Cheers,
Manish

Manish Chhabra
akash negi
Greenhorn

Joined: Feb 24, 2010
Posts: 27
Thanks for your explanation, but how I can I modify it to not change the original (head) and return a complete copy of the head with the modifications in it?

and I understand the 2nd problem with pointer being null now..


Thanks!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
I think you are going to have to use the three bits of high technology I keep telling our undergraduates about, to follow the execution of the method and the state of your list. The thre bits of hardware required are
  • A pencil
  • Some paper
  • An eraser. Experience shows this to be the most important of the three
  • akash negi
    Greenhorn

    Joined: Feb 24, 2010
    Posts: 27
    Campbell Ritchie... You are absolutely right.. I went to my Prof's office hours and he also said that tracing the program helps to debugg it !! He helped me trace the program and pointed the flaws out to me..

    Thanks!!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39882
        
      28
    As I said in your other thread: well done and tell us how you sorted it out.
    akash negi
    Greenhorn

    Joined: Feb 24, 2010
    Posts: 27
    In this program, I was not aware that the line:
    pointer = head;


    copies the reference..

    I thought that the above line actually copies the entire head list into the newly created pointer list.

    My prof. told me that a list can not be copied like that.. we need a loop to do so.

    The code worked previously also but I never understood why it worked, but now I understand how and why it does.
    Thanks!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Weird thing happening in Linked Lists