2014-12-03 6 views
0

LinkedList를 구현해야하고 지금까지 내 목록에 순서대로 값을 삽입하는 메소드를 작성했습니다. 내 클래스의 인스턴스 데이터로 프런트 노드를 가지고 있는데, 첫 번째 값을 만들고 프런트 노드의 값을 새 노드에서 가리키는 값을 설정할 때 nullpointerexception이 발생합니다.LinkedList NullPointerException (LinkedList를 구현하는 데 도움이 필요)

public class Class1 { 
private Node front = null;//Linked List pointer 
private int comparisons = 0; //# of comparisons in Lists 
//private LinkedList<Node> orderLink = new LinkedList<Node>(); 
public Class1() { 

} 

/*public Class1(int x){ 

}*/ 

public void insert (int num){ 
    Node current = null; 
    Node previous = null; 
    boolean placed = false; 
    if (front == null){ //generate first node of list 
     Node first = new Node(num, null); 
     front.setNext(first); 
     placed = true; 
    } 
    previous = front; 
    current = front.getNext(); 
    Node step = new Node(num, null); 
    if (placed == false){ 
    do{ 
     if (step.getData() < current.getData() && step.getData() > previous.getData() || step.getData() == current.getData() || step.getData() == previous.getData()){ //if the new data is between previous and current, place. If equals current, place. 
      //Insert into List 
      step.setNext(current); 
      previous.setNext(step); 
      placed = true; 
     } 
     if (previous == front && step.getData() < current.getData()){ //separate case for first node 
      step.setNext(current); 
      previous.setNext(step); 
      placed = true; 
     } 
     if (current.getNext() == null && step.getData() > current.getData()){ //case for last node 
      current.setNext(step); 
      placed = true; 
     } 
     //move a space up the list 
     previous = current; 
     current = current.getNext(); 

    }while(previous.getNext() != null || placed == false); 
    } 

} 

public int search(int num){ 
    int nodeIndex = 0; 

    return 1; //Return index of num 
} 
public void delete (int num){ 
    //delete num from the list 
} 
public void traverse(){ 
    System.out.println(front.getNext()); 
    System.out.println(front.getNext().getNext()); 
    System.out.println(front.getNext().getNext().getNext()); 
    System.out.println(front.getNext().getNext().getNext().getNext()); 
    System.out.println(front.getNext().getNext().getNext().getNext().getNext()); 
} 
public int getComparisons(){ 
    return comparisons; 
} 

public class Node { 
    private Node next; 
    private int data; 
    public Node(){ 
     next = null; 
    } 
    public Node(int data, Node next){ 
     this.next = next; 
     this.data = data; 
    } 
    public Node getNext(){ 
     return next; 
    } 
    public void setNext(Node nextNode){ 
     next = nextNode; 
    } 
    public int getData(){ 
     return data; 
    } 
    public void setData(int data){ 
     this.data = data; 
    } 
} 

}

public class User { 
public static void main(String[] args){ 
    Class1 test = new Class1(); 
    test.insert(1); 
    test.insert(3); 
    test.insert(5); 
    test.insert(2); 
    test.insert(4); 

    test.traverse(); 
} 

}

난 그냥 LinkedList들을 배우고 시작 그리고 난 내가 잘못 구현하는지 모르는입니다. 어떤 도움이라도 대단히 감사하겠습니다. 그리고 당신은 실제로 내 삽입 메소드와 내부 Node 클래스를 살펴볼 필요가 있습니다.

답변

2

당신은 여기에 문제가 :

front.setNext(first); 

당신이 null 객체에 어떤 작업을 할 수 없습니다 (과 상태에 당신은 앞이 null이). 아마 여기가 있어야한다 :

front = first; 
+1

을, 당신이 그것을 요구 : – realUser404

0

LinkedList의 세 가지 인수를 사용 -> (leftNode, rightNode, 데이터) 이미 앞에서 방금 전에 널 것을 확인할 특히 이후

+4

확장주십시오 대답을 통해 더 많은 정보를 얻을 수 있습니다. –

관련 문제