双重链接列表 - 删除和添加

时间:2013-04-13 02:18:34

标签: java doubly-linked-list

这是一个班级实验室,在其中我试图添加,删除等到双向链表中。我拥有我所相信的正确的东西。如果不是列表的开头或结尾,我在找出如何删除对象时遇到问题。我在使用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);}
 }  

1 个答案:

答案 0 :(得分:2)

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

class DLLNode {
    DLLNode next;
    DLLNode prev;
}

要删除节点,逻辑将为

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

要在节点后面添加节点newNode,逻辑是

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

我没有做任何空指针检查,这取决于你。