2017-11-14 2 views
1

나는 노드의 두 가지 데이터 구조가 주어진다. 그리고 그것들을 교대로 데이터 구조를 가진 하나의 단일 노드로 결합해야한다. 데이터 구조는 길이가 같거나 다를 수 있습니다.데이터 구조를 교대로 노드를 결합하는 방법은 무엇입니까? - Java

이미이 문제를 시도했지만 코드를 검사했지만 여전히 올바르지 않습니다. 이 특정 메서드에서 사용하는 다른 메서드를 작성 했으므로 해당 코드도 게시합니다. 이 특정 문제에 대한 데이터 구성이 어떻게 작동하는지 예제를 통해 내 방법을 생각해 냈습니다.

노드 목록의 머리 받기 :

public static <E> Node<E> getHead(Node<E> current) { 
    Node<E> head = null; 
    while (current != null) { 
     head = current; 
     current = current.previous; 
    } 
    return head; 
} 

는 데이터 구조의 노드의 수를 가져옵니다 :

public static <E> int countNodes(Node<E> current) { 
    int count = 0; 
    while (current != null) { 
     count++; 
     current = current.next; 
    } 
    return count; 
} 

참고 나는이 두 가지 방법 (getHead 및 countNodes)를 테스트 한 그들은 옳은 것으로 입증되었습니다. 나는 그들이 뭔가를 놓치고 있는지보기 위해 그들을 게시했다. (편집을 업데이트)

public static <E> Node<E> combineNodes(Node<E> current, Node<E> current2) { 
    Node<E> newNode = null; 
    int currentSize = countNodes(current); 
    int current2Size = countNodes(current2); 
    int size = Math.max(currentSize, current2Size); 

    for (int i = 0; i < size; i++) { 
     if (i <= currentSize - 1) { 
      Node<E> node = new Node<E>(current.data); 
      newNode.next = node; 
      node.previous = newNode; 
      newNode = newNode.next; 
      current = current.next; 
     } 
     if (i <= current2Size - 1) { 
      Node<E> node = new Node<E>(current2.data); 
      newNode.next = node; 
      node.previous = newNode; 
      newNode = newNode.next; 
      current2 = current2.next; 
     } 
    } 

    return getHead(newNode); 
} 
:

노드 클래스 자체

, 나는이 문제가하고있는 방법을 내 강사에 의해 작성되었습니다 올바르게 마지막으로
public static class Node<E> { 
    public Node<E> next; 
    public Node<E> previous; 
    public E data; 

    public Node(E d) { 
     data = d; 
    } 

    public String toString() { 
     if (next == null) 
      return ""; 
     return data + ", " + next.toString(); 
    } 

    public boolean equals(Object o) { 
     Node<E> node = (Node<E>) o; 
     if (node == null) 
      return false; 
     Node<E> current = this; 
     while (current != null && node != null) { 
      if (!current.data.equals(node.data)) { 
       return false; 
      } 
      current = current.next; 
      node = node.next; 
     } 
     return current == null && node == null; 
    } 
} 

을 기록하기 위해 테스트되었습니다

다시 말하지만 코드를 살펴본 결과 작동해야한다고 생각합니다. 내가 누락하거나 잘못하고있는 것이 있습니까?

내가 주어진 테스트 케이스를 포함해야

편집. 강사가 과제에 JUnit 테스트 케이스 라이브러리를 사용하고 있습니다. 이것은 통과해야하는 테스트 케이스입니다.

@Test 
public void combineNodesTest1() { 
    LinkedData.Node<String> node = makeStructure(10); // Makes a data structure of Nodes from "Fred 0" to "Fred 9" 
    LinkedData.Node<String> node2 = makeStructure(10); 
    LinkedData.Node<String> ret = new LinkedData.Node<String>("Fred 0"); 
    ret.next = new LinkedData.Node<String>("Fred 0"); 
    LinkedData.Node<String> r = ret.next; 
     for(int i = 1; i<10;i++) { 
     r.next = new LinkedData.Node<String>("Fred "+i); 
     r = r.next; 
     r.next = new LinkedData.Node<String>("Fred "+i); 
     r = r.next; 
    } 
    LinkedData.Node<String> answer = LinkedData.combineNodes(node, node2); // Method that I wrote 
    assertEquals(ret, answer); 
} 

makeStructure() 메소드가 올바르게 작성되었습니다. 내가 함께 노드를 실제로 연결하는 코드를 업데이트했습니다,하지만 여전히 잘못이

편집. 나는 지금 내가 뭘 잘못하고 있는지 알고 싶다.

덕분에, 청지기 내 관점에서

+1

내가 부족 뭔가 다음 다음

newNode = current; newNode.next = current2; current = current.next; 

및입니까? 코드가 뭐하는거야? 산출? 그리고 어떻게해야합니까? 몇 가지 예를 제공하십시오 –

답변

0

, 당신은 당신의 combineNodes 방법에 포인터 (다음, 이전)을 변경하지 않습니다. 당신은 구조를 횡단합니다. 무엇 처음 if에서 누락되고 그 결과는 지금 무엇의 두 번째 if

newNode = current2; 
newNode.next = current; 
current2 = current2.next; 
+0

내가 제안한 것을 시도해 보았습니다. 이제 코드를 업데이트했습니다. 여전히 잘못된 것이고 나는 아직도 이유를 파악할 수 없습니다. – user3208915

관련 문제