2016-12-02 26 views
0

我觉得我非常相信我在这段代码中是正确的。从逻辑上讲,这对我来说很合理,但由于某种原因,程序拒绝跑过某个点。我应该这样做,而不使用专有类或哈希表。我的列表节点是一个基本的单向链表。假设我最初有一个虚拟列表,0,我可以将一个数字添加到列表中,但仅此而已。这是除了添加第一个数字之外无法使用的方法。在java中插入一个数字并保留它的排序

假设我的名单是0 - > 2。和我试图加1

public void insert(int newElement) { 
    List marker = head; 
    List temp = new List(newElement, null); 

    if (head.next == null) { 
     head.next = temp; 
    } else { 
     while (marker.next != null) { 
      if (newElement < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       marker = marker.next; 
      } 
     } 
    } 
} 
+2

如果你的头5然后加0会发生什么,你会得到5-> 0 –

+0

列表,以便马上蝙蝠我能想到的是不与您的代码工作的情况。您不会考虑插入的节点可能小于头部。这立即意味着您的列表变为未分类。解决这个问题,如果你有更多的问题回来问这些问题。 – Jay

+0

如果有帮助,这完全是您的逻辑问题,而不是与Java本身有关的微妙或错误。还没有提到的一些直接问题是,如果'newElement> = marker.next.value','while'循环永远不会结束,并且一个元素可能被尝试插入多次,因为它在完成一次后不会停止。 –

回答

0
List marker = head; 
    List temp = new List(newElement, null); 

    if (head.value > temp.value) { 
     head.next = new List(head.value, null); 
     head.value = newElement; 
    }else if(head.next == null) 
    { 
     head.next = temp; 
    } 
    else { 
     while (marker.next != null) { 
      if (newElement < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       break; //we added the node, no need to continue looping 
      } 
      else //we need to iterate to the next node in the list until empty 
      { 
       marker = marker.next; 
      } 
     } 
    } 
+0

我试过用这个。看来我仍然陷入困境。我增加了任何价值,它的工作原理。但是,除此之外的任何价值似乎都没有注册。 – Shaun

0
public void insert(int val) { 
    Item item = new Item(val); 

    // the case when there is no item (not counting the dummy head) 
    if (head.getNext() == null) { 
     head.setNext(item); 
     item.setNext(null); 
    } else { 
     // if there is at least one item.... 
     Item cursor = head.getNext(); 
     Item prev = cursor; 

     // simply keep looping the list until the new value is less than a value in list 
     // if the new value is greater than all the values in the list... 
     // then the do-while loop will break when null is reached... 
     // at the end of the list 
     do { 
      if (val < cursor.getVal()) { 
       // break and insert 
       break; 
      } 
      prev = cursor; 
      cursor = cursor.getNext(); 
     } while (cursor != null); 

     // insert the item 
     item.setNext(cursor); 

     // the case when the new value is the smallest and is to be inserted at head 
     if (val < head.getNext().getVal()) { 
      head = item; 
     } else prev.setNext(item); 
    } 
} 

这是你的代码片段:

if (newElement < marker.next.value) { 
    temp.next = marker.next; 
    marker.next = temp; 
    marker = marker.next; 
} 

乘坐铅笔和纸,并追查这段代码。你会看到它有什么问题。请回答look at this并查看附加的图像。这就是你应该如何真正调试关于链表的问题。该图像不是特定于您的代码,但它应该让您知道如何解决这类问题。

0

这似乎是工作,我目前正在测试它的结果很好。任何人都可以找到我错过的这段代码的问题吗?

public void insert(int newElement) { 
    List marker = head; 
    List temp = new List(newElement, null); 

    if (head.next == null) { 
     head.next = temp; 
    } else { 
     for (marker = head; marker.next != null; marker = marker.next) { 
      if (temp.value < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       break; 
      } 

     } 
     if (marker.next == null && temp.value > marker.value) { 
       marker.next = temp; 

      } 
    } 

} 
+0

按顺序输入这些值并打印输出:'3,4,2,4,1'' – rafid059

+0

输入3,4,2,4,1,结果很好。非常感谢! – Shaun

+0

它不会添加4次,但这仅仅是因为我没有解释它是否与列表中已有的东西相同,但我确信我可以从这里得到它! – Shaun