2014-03-03 30 views
1

我正在研究一个单独链接列表迭代器,并且在调试过程中它不会传递一个for循环,并且我坚持为什么。Java自定义迭代器顽固地陷入每个循环

这是我的迭代器和节点:

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(); 
    } 
} 

要测试迭代和我的链接列表实现我的教练给了我这个:

// 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(); 

这打印出所需的25 ... 1结果然而,当我的迭代器遇到问题是我清空列表并添加1项后:

// 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); 

在调试期间,它无限期停留在for循环中,并且始终不变。 我试图修改我的迭代器,但没有取得任何成功。

这是我在Stack上的第一篇文章,我很抱歉发布任何错误的做法!感谢您的时间!

编辑:

这里是我的节点类:并以不同的任务,我已经证实,它的工作原理使用不同的测试文件。

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

这些都是我的删除方法:

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; 
} 

这里是第二个编辑与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++;} 
+0

? – Kakarot

+0

你能告诉我们Node类吗?你确认它能正常工作吗? – PlasmaPower

+0

编辑回答问题! – Dreaded

回答

0

headtail为空清空列表之后,阅读下面的评论。

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;} 

你能告诉你如何从列表中删除元素你可以将此作为您addLast方法

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++; 
} 
+0

谢谢这么多,这完全解决了它! – Dreaded