为什么这个while循环卡在无限循环中?

问题描述:

我正在处理一个混合数据结构,它是一个有序双链表,其中每个节点都包含一个SIZE数组[]。我的困难是添加方法。为什么这个while循环卡在无限循环中?

使用教授提供的单元测试,测试字符串被分解为单个字符并使用提供的比较器添加到列表中。默认的测试是abcdefghijklmnopqrstuvwxyzaeiou

我写的添加方法增加了第一个项目的罚款,但它不会超过该字符。由于该测试适用于班级中的其他学生,因此它必须是我的代码搞砸了。

我想出了该代码是

boolean add(E item){ 
    Chunk<E> currentNode= head; //set lookup node to the head 

    if (size==0) { 
     //new chunk, connect it with head 
     Chunk<E> newNode= new Chunk<E>(); 
     newNode.next= head; 
     newNode.prev=head; 
     head.next= newNode; 
     head.prev= newNode; 

     //add item and update counters 
     ll.insertItem(newNode, item); 
     size++; 

     //System.out.println(currentNode.items[0]); 
    } 

    else { 
     if (currentNode.next== head) { 
      ll.insertItem(currentNode, item); 
     } 

     while (currentNode.next != head) { 
      currentNode= currentNode.next; 
      System.out.println(currentNode.items[0]); 

      //if the add item is less than first item, move to previous node 
      if (comp.compare(item, currentNode.items[0])<0) 
       currentNode= currentNode.prev; 

      //if item fits within a chunk 
      if (comp.compare(item, currentNode.items[0])> 0 && 
        comp.compare(item, currentNode.items[currentNode.numUsed-1])<0) { 
       ll.insertItem(currentNode, item); 


       //otherwise, move search onto next node 
      } else { 
       currentNode= currentNode.next; 
      } 
     } 
    } 
    return true; 
} 

ll.insertItem在阵列中确定哪个位置插入的项目的嵌套类的辅助方法,并且如果该阵列是满的,拆分节点为两个节点,复制旧的节点到新的后半段,然后添加到相应的node.I该项目实现了它作为

public void insertItem(Chunk<E> node, E item) { 
     if(node.numUsed==0) { 
      node.items[0]=item; 
      node.numUsed++; 

     }else if (node.numUsed<chunkSize) { 
      for (int i=0; i<=node.numUsed; i++) { 
       if (comp.compare(item, node.items[i])>0) { 
        for (int j= node.numUsed; j>i; j--) { 
         node.items[j+1]= node.items[j]; 
        } 

        node.items[i]= item; 
        node.numUsed++; 
       } 
      } 
     } else { 

      //make new chunk, determine which chunk item should be added 
      Chunk<E> newChunk= newChunk(node); 
      size++; 

      //if item fits in new node 
      if (comp.compare(item, newChunk.items[0])>0) { 
       insertItem(newChunk, item); 
      } else { 
       insertItem(node, item); //item fits in old node 
      } 
     } 
    } 

什么我没有变就是该卡在无限循环,特别是在测试字符串的第一个字符上。由于if (size==0)条件执行,为什么代码重复添加一个字符?

Addenum-添加的第一个项目

后RD

System.out.println("insertion order: "+order); 
    Comparator<String> comp = new StringCmp(); 
    ((CmpCnt)comp).resetCmpCnt();   // reset the counter inside the comparator 
    ChunkList<String> clist = new ChunkList<String>(comp); 
    for (int i = 0; i < order.length(); i++){ 
     String s = order.substring(i, i+1); 
     clist.add(s); 
    } 
+0

我们也想看看'insertItem'方法。 – codaddict 2010-10-15 20:45:53

+0

你在编辑帖子时正在编写它 – Jason 2010-10-15 20:47:45

+0

我们可以看到调用add方法的代码吗? – 2010-10-15 21:04:16

你是不是增加numUsed要求是:

if(node.numUsed==0) { 
      node.items[0]=item; 

     } 

应该是:

if(node.numUsed==0) { 
      node.items[0]=item; 
      node.numUsed++; 
     } 
+0

不幸的是,问题仍然存在,因为我将add()方法中的node.num使用更改为node.numUsed-1,以便将项目与空值进行比较。 – Jason 2010-10-15 23:28:04

+0

原来问题完全在其他地方。你的回答解决了眼前的问题。 – Jason 2010-10-18 23:27:08