Question

I am working on a singly linked list iterator and during debugging it won't get passed a for loop and i'm stuck as to why.

Here is my Iterator and Node:

class Node<E> {
    E data;
    Node<E> next;
    public Node(E obj){
        data = obj;
        next = null;
    } //end class node
}

private Node<E> head,tail;
int currentSize = 0;//initializes the size to 0   

class IteratorHelper implements Iterator<E>{
    private Node<E> iteratorptr;

    public IteratorHelper(){
        this.iteratorptr = head;
    }
    public boolean hasNext(){
        return iteratorptr != null && iteratorptr.next != null;
    }
    public E next(){
        if(!hasNext())
            return null;
        iteratorptr = iteratorptr.next;
        return iteratorptr.data;
    }
    public void remove(){
        throw new UnsupportedOperationException();
    }
}

To test the iterator and my linked list implementation my instructor gave me this:

// check the list with the iterator. If n = 25, this should print
// 25 24 23 22 21 ... 5 4 3 2 1
System.out.println("Using the iterator");
for (Integer i : llist)
    System.out.print(i + " ");
System.out.println();

And this prints out the desired 25...1 result however when my iterator runs into problems is after I empty the list and add 1 item to it:

// now add one thing to the list
llist.addLast(n+1);

// this should be the only thing in the list
for (int i : llist)
if (i != (n+1))
    System.err.println("There should be only one thing in the list, but we got " + i);

During debugging it gets stuck in the for loop indefinitely with i = 26 and never changing. I have tried to modify my iterator but haven't had any success.

This is my first post on Stack and I am sorry for any bad-posting practices! Thank you for your time!

Edit:

Here is my Node Class: And in a different assignment I have confirmed that it works using a different test file.

class Node<E> {
    E data;
    Node<E> next;
    public Node(E obj){
        data = obj;
        next = null;
    } //end class node
}

And these are my remove methods:

public E removeFirst() {
    if (head == null)
        return null;
    E tmp = head.data;
    if (head == tail)
        head = tail = null;
    else {
        head = head.next;
    }
    currentSize--;
    return tmp;
}

public E removeLast() {
    Node<E> previous = head;
    if (head == null)
        return null;
    E temp = tail.data;
    if (head.next == null)
        head = tail = null;
    else {
        while (previous.next != tail) {
            previous = previous.next;
        }
        tail = previous;
        tail.next = null;
    }
    currentSize--;
    return temp;
}

Here is the second edit with addLast:

         public void addLast(E obj){
    Node<E> newNode =  new Node<E>(obj);
    if(head == null) head = tail = newNode;
    if(head.next==null){
        head.next = tail.next = newNode;
        tail = newNode;}
    else{
        tail.next = newNode;
        tail = newNode;}
        currentSize++;}
Was it helpful?

Solution

Your head and tail is null after you empty the list, read the comments below.

public void addLast(E obj){
Node<E> newNode =  new Node<E>(obj);
if(head == null) head = tail = newNode; /*You set the head and tail here*/
if(head.next==null){ /*This is obviously null because you only have one node and based on your node class*/
head.next = tail.next = newNode; /*Yet you add this to your next, giving you an infinite loop, a node whose next is itself*/
tail = newNode;}

You can have this as your addLast method

public void addLast(E obj)
{
    Node<E> newNode = new Node<E>(obj);
    if(head == null) 
    {
        head = tail = newNode;
    }   
    else
    {
        tail.next = newNode;
        tail = tail.next;
    }
    currentSize++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top