【问题标题】:Why is this while loop stuck in infinite loop?为什么这个while循环卡在无限循环中?
【发布时间】:2010-10-15 20:40:35
【问题描述】:

我正在研究一种混合数据结构,它是一个有序的双链表,其中每个节点都包含一个 SIZE 的数组 []。我的难点是 add 方法。

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

我编写的 add 方法很好地添加了第一项,但它并没有超过那个字符。由于该测试适用于班上的其他学生,因此一定是我的代码搞砸了。

我想出的代码是

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 是嵌套类中的一个辅助方法,它决定在数组中的哪个位置插入项,如果数组已满,则将节点拆分为两个节点,将旧节点的后半部分复制到new,然后将该项目添加到适当的节点。我将其实现为

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)条件执行了,为什么代码重复添加a字符?

附录 - 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);
    }

【问题讨论】:

  • 我们也想看看insertItem方法。
  • 你是在我编辑帖子时写的
  • 能看到调用add方法的代码吗?
  • 当您使用 IDE 的调试器单步执行代码时会发生什么?

标签: java data-structures infinite-loop


【解决方案1】:

添加第一项后,您没有增加 numUsed

这个:

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

        }

应该是:

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

【讨论】:

  • 不幸的是,问题仍然存在,因为我将 add() 方法中的 node.numUsed 更改为 node.numUsed-1 以更正将项目与空值进行比较。
  • 原来问题完全出在其他地方。您的回答解决了眼前的问题。
猜你喜欢
  • 2018-03-17
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2011-11-15
  • 2016-07-14
  • 2012-05-02
相关资源
最近更新 更多