Results 1 to 1 of 1
  1. #1
    Bungkai is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default 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)


    Java 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);
    
        		}
    }
    Last edited by Bungkai; 12-02-2011 at 07:03 AM.

Similar Threads

  1. Doubly-Linked list
    By swp in forum New To Java
    Replies: 3
    Last Post: 10-11-2011, 03:27 PM
  2. help me with this ! circular doubly linked list
    By someone in forum Advanced Java
    Replies: 1
    Last Post: 04-11-2011, 10:35 AM
  3. Doubly linked list, reverse()
    By hamed in forum New To Java
    Replies: 4
    Last Post: 10-29-2010, 09:53 AM
  4. Doubly Linked List
    By matin1234 in forum New To Java
    Replies: 0
    Last Post: 06-02-2010, 05:58 AM
  5. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •