2017-11-02 43 views
1

전방과 후방에 삽입 할 수있는 이중 링크 목록을 만들뿐 아니라 목록에서 모든 노드를 삭제할 수 있습니다. 문제는 그것이 작동하지 않는다는 것입니다 오프 제공하고 하나는 NullPointerException을 제공하거나 그냥 exist.The 코드는 않지만 정수도 존재하지 않는다고 말한다 :Java의 이중 링크 된 목록에서 노드를 임의로 삭제하는 방법은 무엇입니까?

public class Numbers { 

    Node head = null; //Head of the list 
    Node tail = null; //end of the doubly list  
    int size = 0; 


    public void FrontInsert(int data) { 
     Node n = new Node(); 
     if (head == null) { 
      head = n; 

     } else { 
      n.prev = head; 
      head.next = n; 
      head = n; 

     } 
     size++; 
    } 

    public void RearInsert(int data) { 
     Node n = new Node(); 
     if (head == null) { 
      head = n; 
      tail = n; 

     } else { 

      n.next = tail; 
      tail.prev = n; 
      tail = n; 
     } 
     size++; 
    } 

    public void Delete(int x) { 

     if (size == 0) { 
      System.out.println("The list is empty."); 
     } 
     if (head.data == x) { 
      head = head.next; 
      if (head != null) { 
       head.prev = null; 
      } 
      size--; 
      return; 
     } 

     tmp = head; 

     while (tmp != null && tmp.data != x) { 
      tmp = tmp.next; 
     } 

     if (tmp == null) { 
      System.out.println("That integer does not exist."); 
      return; 
     } 

     if (tmp.data == x) { 
      tmp.prev.next = tmp.next; 
      if (tmp.next != null) { 
       tmp.next.prev = tmp.prev; 
      } 
     } 
     size--; 
    } 
    public void printList() { 
     while (head != null) { 
      System.out.print(head.data + " "); 
      head = head.prev; 
     } 
    } 
    public static void main(String[] args) { 
     Numbers nu = new Numbers(); 

    } 
    class Node { 
     Node prev; 
     Node next; 
     int data; 

     public void Node(int data) { 
      this.data = data; 
      next = null; 
      prev = null; 
     } 
    } 
}  
+0

당신의'당신이'data' 변수를 전달하지 않을 Node'. 'node n = 새로운 노드 (데이터);가되어야합니까? – ChickenFeet

+0

나는 그것을 시도했다, 그것은 삭제 방법을 위해 작동하지 않을 것이다. 호환되지 않는 피연산자 유형 int를 numbers.Node에 대해 얻습니다. – user3394363

+0

코드에 여러 논리 오류가 있습니다. 자신의 교육을 위해 할 수있는 최선의 방법은 한 번에 한 줄씩 IDE 디버거의 코드를 단계별로 실행하고 각 구문 다음에 나오는 변수를 검사하여 무슨 일이 일어나는지 확인하는 것입니다. –

답변

0

이 시도하고 출력

을 확인

공용 클래스 번호 {

Node head = null; 
Node tail = null; 
int size = 0; 

public void FrontInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { // first insert 
     head = n; 
     tail = n; 
    } else { 
     n.next = head; 
     head.prev = n; 
     head = n; 

    } 
    size++; 
} 

public void RearInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = n; 

    } else { 

     n.prev = tail; 
     tail.next = n; 
     tail = n; 
    } 
    size++; 
} 

public void Delete(int index) { // index is the position to be remove 

    if (size == 0) { 
     System.out.println("The list is empty."); return; 
    }else if(index < 0 || index > size -1){ 
     System.out.println("Index outOf Bound."); return; 
    } 

    Node currentNode = head; 
    for(int i = 1; i <= index ; i++){ 
     currentNode = currentNode.next; 
    } 
    //remove 
    if (index == 0) { 
     currentNode.next.prev = null; 
     head = currentNode.next; 
    } else if (index == size - 1) { 
     currentNode.prev.next = null; 
     tail = currentNode.prev; 
    } else { 
     if (currentNode.prev != null) // Ensure its not header 
      currentNode.prev.next = currentNode.next; 
     if (currentNode.next != null) // Ensure its not tail 
      currentNode.next.prev = currentNode.prev; 
    } 
    size--; 
} 

public void printList() { 
    Node tmp = head; 
    while (tmp != null) { 
     System.out.print(tmp.data + " "); 
     tmp = tmp.next; 
    } 
    System.out.println(); 
} 

public static void main(String[] args) { 
    Numbers nu = new Numbers(); 
    nu.FrontInsert(1);nu.printList(); 
    nu.FrontInsert(2);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    System.out.println(); 
    nu.Delete(4); 
    nu.printList(); 
} 

class Node { 
    Node prev; 
    Node next; 
    int data; 

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

}

0
음, 머리와 꼬리, 당신은 목록의 꼬리에 무언가를 추가 할 때 내 말은, 당신 w 상호 배타적이었다

두 측면이 아닌 한쪽면 참조 만 제공하면

tail.next = n; n.prev = tail; and tail = n 등이 있습니다. 여기

는 작업 코드 :

public class Numbers { 

Node head = null; //Head of the list 
Node tail = null; //end of the doubly list  
int size = 0; 


public void FrontInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = head; 
    } else { 
     n.next = head; 
     head.prev = n; 
     head = n; 

    } 
    size++; 
} 

public void RearInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = head; 
    } else { 
     n.next = null; 
     tail.next = n; 
     n.prev = tail; 
     tail = n; 
    } 
    size++; 
} 

@SuppressWarnings("null") 
public void Delete(int x) { 

    if (size == 0) { 
     System.out.println("The list is empty."); 
     return; 
    } 
    if (head.data == x) { 
     head = head.next; 
     if (head != null) { 
      head.prev = null; 
     } 
     size--; 
     return; 
    } 

    Node tmp = head; 


    while (true) { 
     if(tmp == null) 
      break; 
     if(tmp.data == x) 
      break; 
     System.out.println(tmp.data); 
     tmp = tmp.next; 
    } 

    if (tmp == null) { 
     System.out.println("That integer does not exist."); 
     return; 
    } 

    if (tmp.data == x) { 
     tmp.prev.next = tmp.next; 
     if (tmp.next != null) { 
      tmp.next.prev = tmp.prev; 
     } 
    } 
    size--; 
} 
public void printList() { 
    while (head != null) { 
     System.out.print(head.data + " "); 
     head = head.next; 
    } 
} 
public static void main(String[] args) { 
    Numbers nu = new Numbers(); 
    nu.FrontInsert(2); 
    nu.FrontInsert(3); 
    nu.FrontInsert(6); 
    nu.RearInsert(8); 
    nu.RearInsert(20); 
    nu.Delete(8); 
    nu.printList(); 
    // System.out.println(nu.head.data + "data"); 
// System.out.println(nu.head.next.data + "data"); 

} 
class Node { 
    Node prev; 
    Node next; 
    private int data; 


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

} 생성