2016-09-25 6 views
1

リンクリストにinsertAtLast()を実装する必要があります。最後にリンクリストに要素を挿入する方法

私はinsertAtLast()のために書いたコードでNull Pointer Exceptionを取得しています。私が間違ってアクセスしている可能性があります。

正しくアクセスして挿入するにはどうすればよいですか?

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に設定します。 nullは空の参照を意味します。したがって、次の2行のコードでは、メンバー変数「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)
  2. にそれを変更することがあり 、それはノードのオブジェクトを参照しないnull参照であります頭は最後のNodeオブジェクトとの参照を作成します。あなたの作品は新しいノードオブジェクトを持つ参照を作成します。これはリストに何も持たないため、自身をリストに追加しません。
  3. このメソッドの後、ヘッドはListの最初のノードではなくNodeの新しいオブジェクトを参照します。

代替ソリューション:すべての

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; 
} 
関連する問題