2014-10-17 61 views
0

我想将字符串存储在LinkedList中。我不允许预先排序,但找到该位置并将该字符串传递给链接列表。当我通过文本文件传递字符串时,字符串不会通过最后一个条件。 我的输入文件有 乔 APPY appz 斑马 猫找到正确的地方并存储在LinkedList中

当它到达appz,它不通过任何声明去。它应该插入最后一个条件并打印5,但不这样做。

/** 
    * Gets the string and arranges them in order 
    * @param newString 
    */ 
    public void store(String newString) { 

     LinkedListNode current = head; 
     System.out.println(newString); 

     // if no element in the list 
     if (current==null){ 
      System.out.println("1"); 
      makeNode(newString); 
     } 

     // if only 1 elements in the list 
     else if(current.getNext()==null){ 
      System.out.println("2"); 
      if(newString.compareTo(current.getName())<0){ 
       insertBefore(current.getName(),newString); 
      } else{ 
       insertAfter(current.getName(),newString); 
      } 
     } 

     // if the element is smaller than the head in the list 
     else if(newString.compareTo(current.getName()) < 0){ 
      System.out.println("3"); 
      LinkedListNode temp = makeNode(newString); 
      temp.setNext(current); 
      head=temp; 
     } 

     // if the element is greater than the tail in the list 
     else if(newString.compareTo(findTail().getName()) > 0){ 
      System.out.println("4"); 
      insertAfter(findTail().getName(),newString); 
     } 

     // for more than two elements in the list 
     else{ 
      System.out.println("5"); 
      while(!(newString.compareTo(current.getName())>0 && newString.compareTo(current.getNext().getName())<0) && current.getNext()!=null){ 
       current=current.getNext(); 
      } 

      if(newString.compareTo(current.getName())<0){ 
       insertBefore(current.getName(),newString); 
      } 
      else{ 
       insertAfter(current.getName(),newString); 
      } 
     } 

    } // end of store() 
+0

只有appz有这个问题,或者它是从appz和以后的一切吗?如果没有前面的条件执行,它看起来应该执行最后的其他条件。所以无论如何你看它,它应该打印出一个数字 - 对吗? – ucsunil 2014-10-17 18:41:48

+0

@Sunil:亚苏尔,我应该至少去上一条语句,但是在打印5之前停下来,然而程序继续运行。在appz的位置冻结。 – Sooner 2014-10-17 18:43:52

+0

嗯。有趣。由于我没有全部代码,因此我需要你在第一个if(if(current == null)行)到最后一个else(打印5行上面的那一行)的每一行中放置断点。调试程序,你可以让我知道程序停在哪一行?如果一切都是正确的,它应该一路走到最后,但显然这没有发生。让我知道哪一行,或者你可以将所有的代码转储到哪里,我可以看看它 – ucsunil 2014-10-17 18:56:50

回答

0

您对insertBefore有一些问题。我更新了它。

public void insertBefore(String later, String name){ 
     if(head==null){ 
      head = new LinkedListNode(name,null); 
     } 
     else if(head.getName()==later){ 
      LinkedListNode newNode = makeNode(name); 
      newNode.setNext(head); 
      head=newNode; 
     } 
     else{ 
      LinkedListNode current = head; 
      while(current.getNext().getName()!=later){ 
       current=current.getNext(); 
      } 
      LinkedListNode newNode = makeNode(name); // create the new node 
      newNode.setNext(current.getNext());  
      current.setNext(newNode); 
     } 
    } // end of insertBefore() 
+2

谢谢,它的工作!我忘了做新的节点,而是改变了头。 – Sooner 2014-10-17 22:07:52

-1

当你遍历时,你不应该改变头参考。要遍历,只需执行以下操作:

Node tmp = head; 
while(tmp != null) tmp = tmp.next; 

这将变得非常方便,可以确定插入新节点的位置或删除现有节点的位置。

你的类还应该有addFirst,addLast,insertBefore,insertAfter等方法。在下面的代码,对象是任何数据类型的需求(在你的情况,字符串)

public void addLast(Object item) 
{ 
    if(head == null) 
    { 
    addFirst(item); 
    } 
    else 
    { 
    Node<Object> tmp = head; 
    while(tmp.next != null) 
    { 
     tmp = tmp.next; 
    } 
    tmp.next = new Node<Object>(item, null); 
    } 
} 

public void addFirst(Object item) 
{ 
    head = new Node<Object>(item, head); 
} 

public void insertAfter(Object key, Object item) 
{ 
    Node<Object> tmp = head; 
    while(tmp != null && !tmp.data.equals(key)) 
    { 
    tmp = tmp.next; 
    } 

    if(tmp != null) 
    { 
    tmp.next = new Node<Object>(item, tmp.next); 
    } 
} 

public void insertBefore(Object key, Object item) 
{ 
    if(head == null) 
    { 
    return null; 
    } 

    if(head.data.equals(key)) 
    { 
    addFirst(item); 
    return; 
    } 

    Node<Object> previous = null; 
    Node<Object> current = head; 

    while(current != null && !current.data.equals(key)) 
    { 
    previous = current; 
    current = current.next; 
    } 

    //insert between current and previous 
    if(current != null) 
    { 
    previous.next = new Node<Object>(item, current); 
    } 
} 

在我看来,你不应该有一个嵌套的if/else结构弄清楚在何处插入。这应该取决于您所调用的方法。其次,您用于控制代码中执行流程的条件是截然不同的。你的IF条件是如果列表是空的。如果是,则创建一个新节点并将其添加到列表中。该条件之后是检查仅包含一个节点的列表。之后,你不检查列表的长度。预期的逻辑是你应该检查一个大于1的列表大小;然而这是你通过案例(最后一个)的跌幅。如果你打算做这种检查的插入方法外,然后做这样的事情(碰伤你的代码):

if (current==null){ 
    System.out.println("1"); 
    makeNode(newString); 
} 

// if only 1 elements in the list 
else if(current.getNext()==null){ 
    System.out.println("2"); 
    if(newString.compareTo(current.getName())<0){ 
     insertBefore(current.getName(),newString); 
    } else{ 
     insertAfter(current.getName(),newString); 
    } 
} 

// if the list has more than one element 
else 
{ 
    // figure out where it goes (before or after) and insert 
} 

如果你注意到,在else/if和else块基本上做同样的事情。因此,您的代码可以(也应该)简化如下:

​​
+0

老兄,谢谢你,但不帮助我的问题! – Sooner 2014-10-17 20:47:18

相关问题