Linked Lists
Author |
Message |
jamonathin
|
Posted: Wed Nov 07, 2007 10:37 am Post subject: Linked Lists |
|
|
Has anyone here dealt with linked lists in java? I've figured everything out except on how to completely delete an element. I can do all the changing values, but as far as erasing the last node, no such luck.
Simply doing:
currentNode = new intNode (0, null);
or even
currentNode = null;
doesn't seem to work, and ideas? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Wed Nov 07, 2007 10:45 am Post subject: RE:Linked Lists |
|
|
isnt there a method called remove? |
|
|
|
|
|
Tony
|
Posted: Wed Nov 07, 2007 11:50 am Post subject: RE:Linked Lists |
|
|
to remove an object in Java (lets say a node of a linked list), you just have to make sure that nothing points to it -- Java's garbage collector will get to it eventually.
So to remove "current" node, you set "previous" node to point to null. Something like
code: |
prev.setNext(null);
|
doing anything along the lines of
code: |
currentNode = null;
|
doesn't really do anything, since currentNode is most likely just a copy of a pointer -- changing this value will not change the actual object. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
jamonathin
|
Posted: Wed Nov 07, 2007 1:28 pm Post subject: Re: Linked Lists |
|
|
Excellent, thanks Tony. I dont have time at the moment, but i'll implement this later
Im not sure about a remove method 'heavenagain', but i think Tony's idea will cover what i need. |
|
|
|
|
|
|
|