remove() of iterator class for LinkedList

13,210

The issue is that curr doesn't refer to the last element returned, but rather the next element to be returned.

remove() is meant to remove the former, whereas your method removes the latter.

Share:
13,210
CoderNinja
Author by

CoderNinja

Trying to get through college without dying. Love making apps in free time, self teaching Obj-C.

Updated on June 04, 2022

Comments

  • CoderNinja
    CoderNinja almost 2 years

    So I am trying to understand LinkedLists better and an exercise is telling me to add the implement the remove() method of the iterator class for my linked list class that I wrote. My iterator class looks like this:

    public java.util.Iterator<T> iterator() {
        return new java.util.Iterator<T>() {
            Node prev= null,curr = head;
    
            public boolean hasNext() {  
                if (curr != null) {
                    return true;
                }
                return false;
            }
    
            public T next() {
                T temp = curr.data;
                prev = curr;
                curr = curr.next;
                return temp;
            }
    
            public void remove() {
                if(prev==null || curr==null)
                    head=head.next;
                else
                    prev.next=curr.next;
            }
        };
    }
    

    And a test that I wrote for it goes a little something like this:

    public void testiterator(){
        BasicLinkedList<String> basicList = new BasicLinkedList<String>();
        basicList.addToFront("Blue").addToEnd("Red").addToFront("Yellow");
        for(Iterator<String> i = basicList.iterator(); i.hasNext();){
            if(i.next().equals("Blue"))
                i.remove();
        }
        assertTrue(basicList.toString().equals("\" Yellow Red \""));
    }
    

    However when when I print basicList, it tells me that the list contains Yellow and Blue instead of Yellow and Red. Am I implementing the remove() method wrong, am I using it wrong, or both?

    Thanks for your time guys!

  • CoderNinja
    CoderNinja about 11 years
    tries this and it wasn't iterating properly. Had a list of just "yellow" and ran a for(String element: basicList){ System.out.print(element+" "); }
  • CoderNinja
    CoderNinja about 11 years
    I guess that was just a result of my lack of understand on how iterators work and how the remove method is used. I guess I forgot that when .next() is called it shifts to the next one and remove has no way of remembering what the original node was.
  • CoderNinja
    CoderNinja about 11 years
    This pretty much reinforces what I thought was happening. I fixed my code by adding a third Node called prevOriginal. When next() was called prevOriginal = prev before anything was set to the next. Then in the remove I used prevOriginal.next = prev.next to shift the list properly. Crude I know but I guess it works :\ Thanks!
  • vikingsteve
    vikingsteve about 11 years
    Can you post your code for these 3 methods: BasicLinkedList (constructor), addToFront and addToEnd?