2013-04-13 39 views
1

这是一个班级实验室,在其中我尝试添加,删除等到一个双向链接列表。我相信我所拥有的是正确的。如果不是列表的开始或结束,我在解决如何删除对象时遇到问题。我遇到与add方法相似的问题。任何关于从哪里发表意见以及对当前代码的意见都非常感谢。双向链接列表 - 删除和添加

public class Double<T extends Comparable<T>> implements ListInterface<T> { 

protected DLLNode<T> front; //Front of list 
protected DLLNode<T> rear; //Rear of list 
protected DLLNode<T> curPosition; //Current spot for iteration 
protected int numElements; //Number of elements in list 

public Double() { 
    front = null; 
    rear = null; 
    curPosition = null; 
    numElements = 0; 
} 

protected DLLNode<T> find(T target) { 
    //While the list is not empty and the target is not equal to the current element 
    //curr will move down the list. If curr becomes null then return null. 
    //If it finds the element, return it. 
    DLLNode<T> curr; 
    curr = front; 
    T currInfo = curr.getInfo(); 
    while(curr != null && currInfo.compareTo(target) != 0) {  
     curr = (DLLNode<T>)curr.getLink();  
    } 
    if (curr == null) { 
     return null; 
    } 
    else { 
     return curr; 
    }   
} 

public int size() { 
    //Return number of elements in the list 
    return numElements;  
} 

public boolean contains(T element) { 
    //Does the list contain the given element? 
    //Return true if so, false otherwise. 
    if (find(element) == null) { 
     return false; 
    } 
    else { 
     return true; 
    } 
} 

public boolean remove(T element) { 
    //While the list is not empty, curr will move down. If the element can not 
    //be found, then return false. Else remove the element. 
    DLLNode<T> curr; 
    curr = front; 
    T currInfo = curr.getInfo(); 
    while(curr != null) { 
     curr = (DLLNode<T>)curr.getLink();  
    } 
    if (find(element) == null) { 
     return false; 
    } 
    else { 
     if (curr == null) { 
      curr = curr.getBack(); 
      curr = rear; 
     } 
     else if (curr == front) { 
      curr = (DLLNode<T>)curr.getLink(); 
      curr = front; 
     } 
     else if (curr == rear) { 
      curr = curr.getBack(); 
      curr = rear; 
     } 
     else { 

     } 
    return true; 
    } 
} 

public T get(T element) { 
    //Return the info of the find method. 
    if (find(element) == null) { 
     return null; 
    } 
    else { 
     return find(element).getInfo(); 
    } 

} 

//public String toString() { 

//} 

public void reset() { 
    //Reset the iteration back to front 
    curPosition = front;   
} 

public T getNext() { 
    //Return the info of the next element in the list 
    DLLNode<T> curr; 
    curr = front; 
    //while (curr != null) { 
     //curr = (DLLNode<T>)curr.getLink(); 
    //} 
    if ((DLLNode<T>)curr.getLink() == null) { 
     return null; 
    } 
    else { 
     curr = (DLLNode<T>)curr.getLink(); 
     return curr.getInfo(); 
    }  
} 

public void resetBack() { 

} 

public T getPrevious() { 
    //Return the previous element in the list 
    DLLNode<T> curr; 
    curr = front; 
    if ((DLLNode<T>)curr.getLink() == null) { 
     return null; 
    } 
    else if((DLLNode<T>)curr.getBack() == null) { 
     return null; 
    } 
    else { 
     curr = curr.getBack(); 
     return curr.getInfo(); 
    } 
} 

public void add(T element) { 
    //PreCondition: Assume the element is NOT already in the list 
    //AND that the list is not full! 
    DLLNode<T> curr;     
    DLLNode<T> newNode = (DLLNode<T>)element; 
    curr = front; 
    if (curr == null) { 
     front = (DLLNode<T>)element; 
    } 
    else { 

    } 

    } 
} 

这是最终目标主要功能:

public static void main(String[] args){ 

Double<String> d = new Double<String>(); 
d.add("Hello"); 
d.add("Arthur"); 
d.add("Goodbye"); 
d.add("Zoo"); 
d.add("Computer Science"); 
d.add("Mathematics"); 
d.add("Testing"); 

System.out.println(d); 

System.out.println("Contains -Hello- " + d.contains("Hello")); 
System.out.println("Contains -Spring- " + d.contains("Spring")); 
System.out.println("size: " + d.size()); 
d.resetBack(); 
for (int i = 0; i < d.size(); i++){ 
    String item = (String)d.getPrevious(); 
    System.out.println(item); 
    } //good stopping point 
d.remove("Zoo"); 
d.remove("Arthur"); 
d.remove("Testing"); 
System.out.println("size: " + d.size()); 
System.out.println(d); 
d.remove("Goodbye"); 
d.remove("Hello"); 
System.out.println(d); 
d.remove ("Computer Science"); 
d.remove("Mathematics"); 
System.out.println(d);} 
} 

回答

2

我不知道你的DLLNode类是什么样子,但它可能看起来像

class DLLNode { 
    DLLNode next; 
    DLLNode prev; 
} 

要删除一个节点,逻辑将是

next.prev = prev; 
prev.next = next; 

添加节点newNode下一个节点,逻辑是

newNode.prev = this; 
newNode.next = next; 
next = newNode; 

我没有做过任何空指针检查,这是给你的。

+0

是的,我发现了一些让我尝试的东西。我只是有问题让我的toString现在开始工作。 toString工作后,我将能够看到我的添加/删除工程 – TheFatness