【问题标题】:Doubly Linked List -Removal and Adding双向链表 - 删除和添加
【发布时间】:2013-04-13 02:18:33
【问题描述】:

这是一个课堂实验室,我在其中尝试将添加、删除等添加到双向链表中。我拥有我认为正确的东西。如果对象不是列表的开头或结尾,我无法弄清楚如何删除它。我对 add 方法有类似的问题。非常感谢任何关于从这里去哪里以及我当前代码上的 cmets 的建议。

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);}
 }  

【问题讨论】:

    标签: java doubly-linked-list


    【解决方案1】:

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

    class DLLNode {
        DLLNode next;
        DLLNode prev;
    }
    

    要删除一个节点,逻辑是

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

    在一个节点之后添加一个节点newNode,逻辑是

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

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

    【讨论】:

    • 是的,我发现了一些让我尝试的东西。我只是在让我的 toString 现在工作时遇到问题。在 toString 工作后,我将能够查看我的添加/删除是否有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多