java - Linked list .clear() method not working properly -
I have to make my own double linked list. I am making a beginning, so please forgive me for lack of knowledge. This list is to implement the list Java interface, so I have an exhaust, an extract and a clear method, from which the clear () method does not work, which means that it Do not remove all the elements in the list, only something
Here is the obvious () method:
public (for clear) (for (T: this) {this.remove (t);} This.remove (This.size);}
Remove (object) method:
Remove public boolean (object o) {if (this.indexOf (o) > = 0) {Remove (this.indexOf (o)); return true;} description is false;}
And finally, remove (int) method:
< Pre> remove public T {int (index) = if {getNode (index) == null | | This.isEmpty ()) {return tap;} and if (this.size == 1) {this.size = 0; n & gt; CurrentNode = this.firstNode; This.firstNode = Null; This.NlastNode = Null; return currentNode.data;} node CurrentNode = this.getNode (index); if (currentNode.nextNode! = Null) {if (currentNode.previousNode! = Null) {CurrentNode.previousNode.nextNode = currentNode.nextNode;} and {this.firstNode = currentNode.nextNode; This.firstNode.previousNode = null; this.size--; return currentNode.data;}} If (currentNode.previousNode! = Null) {if (currentNode.nextNode! = Null) {currentNode.nextNode.previousNode = currentNode.previousNode; } Other {this.lastNode = currentNode.previousNode; This.lastNode.nextNode = Null; This.size--; Return CurrentNode.data; }} CurrentNode = currentNode.nextNode; This.size--; For (int i = index; i Please not only mention the bug, but rather help me improve this code, if you can thank you for your effort!
Your clear ()
method is very suspicious that it The list removes an element from
, while that list is being circulated if you have tried to do this, then with one of the built in list
implementation, Get the concurrent correction code
. It's really difficult to implement the list
which can handle this type of thing properly.
In any case, I usually use a Java linked method to cancel the context of clear ()
list of any nodes, such as your < Code> remove (int) method when extracting the only element in your list clear ()
should be able to do this in relation to the contents of the list.
Edit to add:
Specifically, since you asked, it seems that you
Public can use the obvious () clear {this.firstNode = null; This.NlastNode = Faucet; This.size = 0; }
( Use this.
is unnecessary, and I do not usually do it, but I'm following the style of your second code. ) Note that I do not have enough information that this is 100% correct and sufficient for your implementation, so I initially did not include specific code.
Comments
Post a Comment