2013-04-05 3 views
0

이 사이트에서 검색 엔진을 쿼리했지만 찾고자하는 내용과 일치하는 내용이 표시되지 않았으므로이 질문이 이미 다른 곳에서 응답되지 않았기를 바랍니다. 정렬 된 LinkedList에 대한 add 메서드를 완벽하게하려고합니다. 프로그램의 나머지 부분은 정상적으로 실행되고 목록이 테스트 장치에 지정된대로 인쇄되지만 이름을 정렬해야합니다. 고소, 고소, 빌, 마이클, 마이클, 칼, 칼, 스티브 등 :노드 사이에 요소 추가 Ordered LinkedList

Sue, Bill, Michael, Someguy, Michael, Carl, Steve, Carl, Sue 
Sue, Bill, Someguy, Michael, Steve, Carl, Sue 
Sue, Bill, Someguy, Michael, Steve, Carl, Sue, Sue, Bill 

는 내가 원하는 것은 이것이다 :

내 출력이 추가 하네스에서 제거 몇 통화 이후 ...

내 추가 방법 :

public boolean add(Comparable obj) 
{ 
    OrderedListNode newNode = new OrderedListNode(obj, null, null); 
    if(tail == null) { 
    tail = newNode; 
    head = tail; 
    modCount++; 
    return true; 

    } 
    if(((Comparable)(head.theItem)).equals(obj)){ 
     tail.previous = newNode; 
     modCount++; 
     return true; 
    }else{ 


    tail.previous.next = newNode; 
    newNode.previous = tail.previous; 
    newNode.next = tail; 
    tail.previous = newNode; 
    modCount++; 
    return true; 
    } 
} 

전체 코드, 그것은 요구 이후 :

package dataStructures; 


public class OrderedLinkedList 
{ 

/************************************************************************** 
* Constants 
*************************************************************************/ 

/** return value for unsuccessful searches */ 
private static final OrderedListNode NOT_FOUND = null; 


/************************************************************************** 
* Attributes 
*************************************************************************/ 

/** current number of items in list */ 
private int theSize; 

/** reference to list header node */ 
private OrderedListNode head; 

/** reference to list tail node */ 
private OrderedListNode tail; 

/** current number of modifications to list */ 
private int modCount; 


/************************************************************************** 
* Constructors 
*************************************************************************/ 


/** 
* Create an instance of OrderedLinkedList. 
* 
*/ 
public OrderedLinkedList() 
{ 
    // empty this OrderedLinkedList 
    clear(); 
} 


/************************************************************************** 
* Methods 
*************************************************************************/ 


/* 
* Add the specified item to this OrderedLinkedList. 
* 
* @param obj  the item to be added 
*/ 
public boolean add(Comparable obj) 
{ 
    OrderedListNode newNode = new OrderedListNode(obj, null, null); 
    if(tail == null) { 
    tail = newNode; 
    head = tail; 
    modCount++; 
    return true; 

    } 
    if(((Comparable)(head.theItem)).compareTo(obj) > 0){ 
     //////////////////////////////////////// 
     //here is where my problem lies, I believe 
     ///////////////////////////////////////// 
     modCount++; 
     return true; 
    }else{ 


    tail.previous.next = newNode; 
    newNode.previous = tail.previous; 
    newNode.next = tail; 
    tail.previous = newNode; 
    modCount++; 
    return true; 
    } 
} 

/* 
* Remove the first occurrence of the specified item from this OrderedLinkedList. 
* 
* @param obj  the item to be removed 
*/ 
public boolean remove(Comparable obj) 
{ 
    if(head == null) return false; 
    if(((Comparable)(head.theItem)).compareTo(obj) == 0) { 
    if(head == tail) { 
     head = tail = null; 
     return true; 
    } 
    head = head.next; 
    return true; 
    } 

    if(head == tail)return false; 
    OrderedListNode ref = head; 
    while(ref.next != tail) { 
     if(((Comparable)(ref.next.theItem)).compareTo(obj) == 0) { 
      ref.next = ref.next.next; 
      return true; 
     } 
     ref = ref.next; 
    } 
    if(((Comparable)(tail.theItem)).compareTo(obj) == 0) { 
     tail = ref; 
     tail.next = null; 
     return true; 
    } 

    return false; 

} 


/** 
* Empty this OrderedLinkedList. 
*/ 
public void clear() 
{ 
    // reset header node 
    head = new OrderedListNode("HEAD", null, null); 

    // reset tail node 
    tail = new OrderedListNode("TAIL", head, null); 

    // header references tail in an empty LinkedList 
    head.next = tail; 

    // reset size to 0 
    theSize = 0; 

    // emptying list counts as a modification 
    modCount++; 
} 


/** 
* Return true if this OrderedLinkedList contains 0 items. 
*/ 
public boolean isEmpty() 
{ 
    return theSize == 0; 
} 


/** 
* Return the number of items in this OrderedLinkedList. 
*/ 
public int size() 
{ 
    return theSize; 
} 


/* 
* Return a String representation of this OrderedLinkedList. 
* 
* (non-Javadoc) 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() 
{ 
    String s = ""; 

    OrderedListNode currentNode = head.next; 

    while (currentNode != tail) 
    { 
     s += currentNode.theItem.toString(); 

     if (currentNode.next != tail) 
     { 
      s += ", "; 
     } 

     currentNode = currentNode.next; 
    } 

    return s; 
} 

private static class OrderedListNode<Comparable> { 

    Comparable theItem; 
    OrderedListNode<Comparable> next; 
    OrderedListNode<Comparable> previous; 

    public OrderedListNode(Comparable theItem, OrderedListNode<Comparable> previous, OrderedListNode<Comparable> next) { 
     this.theItem = theItem; 
     this.next = next; 
     this.previous = previous; 
    } 

} 

}

+0

하나의 방법을 보면 이해할 수 없습니다. 출력이 어떻게 형성되는지 볼 수있는 충분한 코드를 추가하십시오. 당신의 "* 나는 이름이 분류되기를 바란다"는 것이 명확하지 않다는 것을 의미합니다. –

+0

도움이 될지 모르겠 음 – Iridan

답변

0

이 줄은 분명히 잘못

if(((Comparable)(head.theItem)).equals(obj)) { 

당신은 OBJ와 인서트 요소 크거나 같은 찾을 때까지 당신은 항상 머리에서 시작하고 가야 외에 당신이 Comparable.compareTo를 사용한다 obj before this, 이와 비슷한 것

public boolean add(Comparable obj) { 
    modCount++; 
    if (head == null) { 
     head = new OrderedListNode(obj, null, null); 
     return true; 
    } 
    for (OrderedListNode current = head; current != null; current = current.next) { 
     if (((Comparable) (current.theItem)).compareTo(obj) >= 0) { 
      current.prev = new OrderedListNode(obj, current.prev, current); 
      return true; 
     } 
    } 
    tail.next = new OrderedListNode(obj, tail, null); 
    return true; 
} 
+0

그 머리를 인쇄하지만 그걸 가지고 놀아 보겠습니다. 감사합니다 – Iridan

+0

그래서 newNode를 가져 와서 객체가 비교되는 다음 노드에 할당하고 싶습니다. 나는 그것을 올바르게보고 있는가? – Iridan

+0

내가하려는 것은 추가 된 항목을 정렬하는 것이므로 동일한 항목을 비교해서는 안됩니까? > 0 연산자를 사용해야하는 이유는 무엇입니까? 내가 모르는 것 때문에 물어 본다. 내가 너와 논쟁하지 않기 때문이 아니다. – Iridan