java - Circular Double Linked List Infinite Loop -
my cs professor asked develop our own java programme using circular linked lists. project add together or delete names (of type string) circular list. far, add together methods work perfectly; however, removenode() method not work , not remove desired element. goes on infinite loop , have tried many pieces of code , neither of them work. remove method following:
public e removenode(e nodetobedeleted) { node<e> nodefound = findnode(nodetobedeleted); if(nodefound != null) { nodefound.prev.next = nodefound.next; nodefound.next.prev = nodefound.prev; size--; homecoming nodefound.data; } homecoming null; } basically, findnode() searches node info equal string plugged in parameter, when phone call outputlist() method, returns string representation of current nodes on screen, goes on infinite loop.
the outputlist method is:
public void outputlist() { node<e> position = head; { system.out.print(position.data + " ==> "); position = position.next; } while((position != null) && (position.next != position)); } any help highly appreciated.. in advance.
the node class is:
static class node<e> { /** info value. */ private e data; /** link next node. */ private node<e> next = null; /** link previous node. */ private node<e> prev = null; private node(e dataitem) { info = dataitem; } private node(e newdata, node<e> noderef) { info = newdata; next = noderef; } private node(node<e> prevref, e newdata) { info = newdata; prev = prevref; } //set next link private node(node<e> newdata, node<e> noderef) { info = (e) newdata; next = noderef; } } //end class node while((position != null) && (position.next != position))
this should be:
while((position != null) && (position.next != head)) imagine if have singleton - absolute base of operations case traversal. head , position both pointing when start, , when wish advance, position refer same place head 1 time again. go on ad infinitum.
the iteration must stop when you've reached starting point 1 time again.
java
No comments:
Post a Comment