2016-12-06 128 views
2

我正在阅读关于java中的链表,我编写了这个代码,当我运行它时,我得到了3作为输出,它应该打印出插入数字的反向顺序如下:10 7 3 ..我的代码有什么问题?在java中实现一个链表在

public class List { 
    private class ListNode { 
     private int contents; 
     private ListNode next; 

     ListNode(int contents) { 
      this.contents = contents; 
      this.next = null; 
     } 
    } 

    private ListNode head; 

    public List() { 
     head = null; 
    } 

    public void insert(int contents) { 

     ListNode newNode = new ListNode(contents); 
     if (head == null) { 
      head = newNode; 
     } 

     ListNode current = head; 
     while (current != null) { 
      current = current.next; 
     } 

     current = newNode; 

    } 

    public void reverse() { 

     ListNode current = head; 
     ListNode nodeNext = current.next; 

     while (nodeNext != null) { 
      current.next = null; 
      nodeNext.next = current; 
      current = nodeNext; 
     } 

     head = current; 
    } 

    public String toString() { 

     String s = ""; 
     ListNode current = head; 
     while (current != null) { 
      s = current.contents + s; 
      current = current.next; 
     } 

     return s; 
    } 

    public static void main(String[] args) { 

     List l = new List(); 
     l.insert(3); 
     l.insert(7); 
     l.insert(10); 
     l.reverse(); 
     System.out.println(l.toString()); 

    } 
} 

感谢

+1

你从来没有设置'插入时next'值。使用调试器,您很快就会发现这一点。 – jhamon

+0

你的意思是这样的'public void insert(int contents,ListNode next){' –

+0

你是怎么计划这个的?你有没有写出你的想法,或者你先在纸上做过这些? – AxelH

回答

1
private ListNode head; 
private ListNode tail; 
public void insert(int contents) { 

    ListNode newNode = new ListNode(contents); 
    if (head == null) { 
     head = newNode; 
     tail = newNode; 
     return; 
    } 
    tail.next = newNode; 
    tail = newNode; 
} 

保持为O(1)插入一个尾节点参考。

reverse()方法是有点不对:

// this loop will run forever if there are more than 1 nodes 
while (nodeNext != null) { 
    current.next = null; 
    nodeNext.next = current; // you lose the reference to the entire list here 
    current = nodeNext; 
} 

重写功能:

public void reverse() { 
    ListNode cursor = head; 
    ListNode newHead = null; 

    while (cursor != null) { 
     ListNode next = cursor.next; 
     cursor.next = newHead; 
     newHead = cursor; 
     cursor = next; 
    } 
    head = newHead; 
} 

cursor.next = newHead,失去原有的参考cursor.next。所以你需要采取临时变量的cursor.next参考:ListNode next = cursor.next;

打印功能

public void print() { 
    ListNode cursor = head; 
    while (cursor != null) { 
     System.out.println(cursor.contents); 
     cursor = cursor.next; 
    } 
} 
+0

谢谢..为什么当我写'System.out.println(l。reverse());'在主要方法eclipse中说:'PrintStream类型中的方法println(boolean)不适用于参数(void)' –

+0

@senshinakamora因为我写的反向方法不会返回任何东西。该类型是'void'。您正将'void'类型(不是类型)传递给'println()',并且PrintStream中没有任何方法需要void参数。 Eclipse采用了第一种使用布尔值的println,因此它给出了错误消息 – rafid059

+0

我已经添加了打印功能。调用'l.reverse()',然后调用'l.print()'。 – rafid059

4

insert法新节点无法连接到现有列表的最后一个节点。你必须到新节点分配给现有列表的最后一个节点的node.next

public void insert(int contents) { 

    ListNode newNode = new ListNode(contents); 
    if (head == null) { 
     head = newNode; 
     return; 
    } 

    ListNode current = head; 
    while (current.next != null) { 
     current = current.next; 
    } 

    current.next = newNode; 

} 
+1

:'空指针访问:变量当前只能为null在这个位置'代码不起作用谢谢 –

+1

@senshinakamora你有没有注意到我把循环的条件改成'while(current.next!= null)'? – Eran

+0

不,我没有抱歉..现在当我运行它,我没有看到任何东西在输出中..有什么问题,我的'toString'方法?谢谢 –