Doubly Linked List removing help?
I have a tester which helps me keep track of how many elements I have in the list....
insert("B", "B", "B");
insert("C", "C", "C");
insert("D", "D", "D");
System.out.println(DLL.size());
balls.remove("C");
System.out.println(DLL.size());
balls.remove("D");
System.out.println(DLL.size());
Gives the following result:
3
2
1
Which works correctly, but when I go into the program and put outputs within my for loop, I get this..
3
B
C <------(stops at c which is correct...supposed to remove after remove("C")
2
B
C <------- (comes back..
D
1
Meaning that my code is NOT ACTUALLY REMOVING ANYTHING!!
I'm guessing somewhere in my code that there is a missing reference. I cannot find it :(
Can anyone help out? (Assume the comparator works because it does for me and that all variables are already declared elsewhere in the program)
Code:
public E remove(K key)
{
keyComparator<K> compKey = new keyComparator<K>(keyComparator);
if(this.isEmpty())
return null;
if(!this.isEmpty())
{
Node keyCurrent = new Node();
//keyCurrent.key = key;
keyCurrent = this.keyHead;
System.out.println(keyCurrent.key);
for(int i=0;i<=this.size;i++)
{
if(compKey.compare(key, keyCurrent.key) == 0)
{
if(keyCurrent.prev == null)
{
keyCurrent.next.prev = null;
keyCurrent = keyCurrent.next;
keyHead = keyCurrent;
this.size--;
return null;
}
else if(keyCurrent.next == null)
{
keyCurrent.prev.next = null;
keyCurrent = keyCurrent.prev;
keyTail = keyCurrent;
this.size--;
return null;
}
keyCurrent.prev.next = keyCurrent.next;
keyCurrent.next.prev = keyCurrent.prev;
this.size--;
return null;
}
keyCurrent = keyCurrent.next;
System.out.println(keyCurrent.key);
}
}