2

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!

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

Why not just set the current to the next node. Why check for null in the remove method.

1 Comment

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.
0

Try this mate:

public java.util.Iterator<T> iterator() {
    return new java.util.Iterator<T>() {
        Node<T> prev = null;
        Node<T> curr = null;

        public boolean hasNext() {
            if (curr == null) {
                return (head != null);
            }
            return (curr.next != null);
        }

        public T next() {
            if (!hasNext()) {
                return null;
            }
            if (curr == null) {
                curr = head;
            } else {
                prev = curr;
                curr = curr.next;
            }
            return curr.data;
        }

        public void remove() {
            if (curr != null) {
                if (prev != null) {
                    prev.next = curr.next;
                } else {
                    head = curr.next;
                }
            }
        }
    };
}

2 Comments

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+" "); }
Can you post your code for these 3 methods: BasicLinkedList (constructor), addToFront and addToEnd?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.