2016-09-25 99 views
1

我必須在鏈表中實現insertAtLast()如何在鏈表中最後插入元素

我得到一個空指針異常代碼,我寫了insertAtLast()。它可能是因爲我訪問錯了。

如何正確訪問它並進行插入?

class myLinkedList { 
private static class Node 
{ 
    private int data; 
    private Node next; 

    public Node(int data, Node next) 
    { 
     this.data = data; 
     this.next = next; 
    } 
} 

private Node head; 

// Constructs an empty list 
public myLinkedList() 
{ 
    head = null; 
} 

// Returns true if the list is empty otherwise returns false 
public boolean isEmpty() 
{ 
    return (head == null); 
} 

// Inserts a new node at the beginning of this list.  
public void insertAtBeginning(int element) 
{ 
    head = new Node(element, head); 
} 

// Returns the first element in the list. 
public int getFirstElement() 
{ 
    if(head == null) 
    { 
     System.out.println("Empty linked list"); 
     throw new IndexOutOfBoundsException(); 
    } 
    return head.data; 
} 

// Removes the first node in the list. 
public int removeFirstNode() 
{ 
    int tmp = getFirstElement(); 
    head = head.next; 
    return tmp; 
} 

// Empties linked list 
public void clear() 
{ 
    head = null; 
} 

// Returns the length of the linked list 
public static int LLlength(Node head) 
{ 
    int length = 0; 
    Node currentNode = head; 

    while(currentNode != null) 
    { 
     length++; 
     currentNode = currentNode.next; 
    } 
    return length; 
} 

// Displays the linked list elements 
public static void display(Node head) 
{ 
    if(head == null) 
    { 
     System.out.println("Empty linked list"); 
     throw new IndexOutOfBoundsException(); 
    } 

    Node currentNode = head; 

    while(currentNode != null) 
    { 
     System.out.print(currentNode.data+" "); 
     currentNode = currentNode.next; 
    } 
    System.out.println(); 
} 

// Displays the linked list elements in reverse order 
public static void displayReverse(Node head) 
{ 
    if(head == null){} 
    else 
    { 
     Node currentNode = head; 
     displayReverse(currentNode.next); 
     System.out.print(currentNode.data+" "); 
    } 
} 
//Displays the linked list's last element 
public static int getLastElement(Node head) 
{ 
    Node currentNode = head; 

    while(currentNode.next != null) 
    { 
     currentNode = currentNode.next; 
    } 
    return currentNode.data; 
} 
public static void insertAtLast(Node head,int element) 
{ 
    Node newNode=null; 
    newNode.data = element; 
    newNode.next = null; 
    while(head.next != null) 
    { 
     head = head.next; 
    } 
    head = newNode; 
    //return head; 

} 

//Tells if a sepeific element is in the Linked List or not 
public static boolean searchFor(Node head, int element) 
{ 
    Node currentNode = head; 
    boolean flag = false; 
    while(currentNode != null) 
    { 
     if (currentNode.data == element) 
     { 
      flag = true; 
      break; 
     } 
     currentNode = currentNode.next; 
    } 
    return flag; 
} 

} 

回答

1

看看你如何聲明節點對象newNode。您將它設置爲等於空而不是將其實例化爲實際對象。請記住,null表示空引用。因此,接下來的兩行代碼會給你一個錯誤,因爲你試圖訪問「nothing」的成員變量。既然你有一個Node類和構造函數,那就用它來代替你現在正在做的事情。最後,使用指向head的temp變量並使用該temp指針遍歷節點。因爲在你面前了,你會設置頭指向鏈表中的新的對象,這是最後一個元素,而不是第一個,

public static void insertAtLast(Node head,int element) 
{ 
    Node newNode= new Node(element,null); 
    Node temp = head; 
    while(temp.next != null) 
    { 
     temp = temp.next; 
    } 
    temp.next = newNode; 
    //return head; 

} 
1

存在着麻煩您insertAtLast方法。


  1. 節點newNode = NULL;newNode對象爲空引用它不參考節點, 的對象你可能把它變成節點newNode =新節點(值,NULL)您while循環
  2. 頭部會對最後一個Node對象做一個引用,你的作品只會使頭部與新的Node對象引用,它對列表self不起作用,因爲這個新對象與list沒有任何關係。
  3. 在此方法之後,頭部將引用Node的新對象而不是List的第一個對象。

的替代解決方案:

public static void insertAtLast(Node head,int element) 
{ 
    Node newNode=new Node(0,null); 
    newNode.data = element; 
    newNode.next = null; 
    Node temp = head; 
    while(temp.next != null) 
    { 
     temp = temp.next; 
    } 
    temp.next = newNode; 
    //return head; 
} 
0

首先,爲什麼是一些你的方法static?他們有意義,而不是static

如果你想支持添加到鏈表的末尾,我建議你跟蹤你的列表的tail。現在你的insertAtLastO(n),你可以通過存儲一個指向你的鏈表的末尾的指針,很容易地將其更改爲O(1)

但它會增加許多其他方法的複雜性。所以如果你決定使用tail指針,你可能需要更新其他的。我不打算詳細介紹其他方法,但只給出你的非靜態的insertAtLast看起來像是如果你有一個tail指針。

public void insertAtLast(int element) 
{ 
    Node newNode = new Node(element,null); 

    if (head == null){ // you need to check for head, it may be null 
     head = newNode; 
    } 

    if (tail == null){ // also, maybe there is no tail currently. 
     tail = newNode; 
     return; 
    } 

    //there is an existing tail, update it 
    tail.next = newNode; 
    tail = newNode; 
}