簡體   English   中英

用雙向鏈表測試問題

[英]Testing Issues with Doubly Linked Lists

我寫了下面的代碼作為一個雙向鏈表:

節點類:

 public class MyDoubleNode<AnyType> {
    public AnyType data; //value of the node
    public MyDoubleNode<AnyType> next; //points to the next node
    public MyDoubleNode<AnyType> prev; //points to the previous node
    public static void main(String[] args) {

    }
}

接口類:

public interface DoublyLinkedList<AnyType> {
    public void insert(AnyType x);
    public void delete(AnyType x);
    public boolean contains(AnyType x);
    public AnyType lookup(AnyType x);
    public boolean isEmpty();
    public void printList();
    public void printListRev();
}

雙向鏈表類:

public class OwnDoublyLinkedList<E> implements DoublyLinkedList {
    protected MyDoubleNode head;
    protected MyDoubleNode tail;
    protected MyDoubleNode front = null; //checks to see that front is the first node


public OwnDoublyLinkedList(){ //sets up constructor
    head = new MyDoubleNode();
    tail = new MyDoubleNode();
    head.prev = null;
    head.next = tail;
    tail.prev = head;
    tail.next = null;

}

@Override
public void insert(Object obj) { //inserts a new node
    MyDoubleNode newNode = new MyDoubleNode();
    newNode.data = obj; //sets a new node to what the object is
    if (contains(obj) == false){ //if this is not a duplicate
        if (head.prev == null){ //if this is the first node
            head.data = newNode;
        }
        else {
            (newNode.prev).next = newNode; //connects the node behind it
            (newNode.next).prev = newNode; //connects the node in front of it
        }
    }

}

@Override
public void delete(Object x) {
    if (contains(x) == true){ //if it is in the list
        head.prev = head.next; //sets the previous head to the next head
        tail.prev = tail.next; //sets the previous tail to the next tail
    } //this cuts out the node from the list
}

@Override
public boolean contains(Object x) {
    MyDoubleNode front = null; //checks to see that front is the first node
    if (head.prev == null){ //sets it to the value of the first node
        front = head;
    }
    while (front!= null){
        if (x.equals(front.data)){ //if the object is in the list, it returns true
            return true;
        }
        front = front.next;
    }
    return false; //if the object is not in the list, it returns false
}

@Override
public Object lookup(Object x) {
    if (head.prev == null){ //sets it to the value of the first node
        front = head;
    }
    while (front!= null){
        if (x.equals(front.data)){ ////if the node equals the data were looking for, return the object
            return front.data;
        }
        front = front.next;
    }
    return null; //if the object isnt in the list, return null
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return head.data == null;
}

public void printList() {
    if (head.data == null){
        System.out.println("null");//sets it to the value of the first node

    }
    else if (head.data != null){
        System.out.println(head.data);

    }
    else if (tail.prev == null){
        front = head;
        front.data = head.data;
    }
    while (front!= null){
        System.out.println((front.data).toString()); //prints the data until theres none left
        front = front.next;
    }
}

public void printListRev() {
    MyDoubleNode front = null;
    if (tail.next == null){ //if this is  the last node, set back to the node
        front = head;
    }
    while (head.prev != null){
        System.out.println((front.data).toString()); //prints the data until there's none left, but backwards
        front = front.prev;
    }
}
}

測試類:

public class Lab5Tester {
    public static void main (String [] args){
            OwnDoublyLinkedList tester = new OwnDoublyLinkedList();
            tester.insert(1);
            tester.insert(3);
            tester.printList(); //runtime of O(n)
            tester.printListRev(); //runtime of O(n)
            System.out.println(tester.contains(1));
            System.out.println(tester.isEmpty());
    }
}

如果我的代碼有點荒謬,我深表歉意。 我剛剛學習了泛型和鏈表,並沒有完全理解它。 當我運行我的測試代碼時,我得到節點在內存中的位置,而不是節點上的數據。 我該如何解決這個問題,以便它打印我添加到列表中的數字?

最好的答案是Object.toString()的文檔

返回對象的字符串表示形式。 通常, toString 方法返回一個“文本表示”此對象的字符串。 結果應該是一個簡潔但信息豐富的表示,易於人們閱讀。 建議所有子類都覆蓋此方法。

Object 類的 toString 方法返回一個字符串,該字符串由對象是其實例的類的名稱、at 符號字符“@”和對象哈希碼的無符號十六進制表示組成。 換句話說,此方法返回一個等於以下值的字符串:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

真正的問題是您正在為數據分配節點。 我看到的輸出(我足以運行你的 MCVE 而不僅僅是猜測)是MyDoubleNode@7f31245a但我知道你只打印.data值,這意味着一個節點最終出現在.data 果然:

head.data = newNode;

這應該是:

head = newNode; //or
head.next = newNode; //etc

您還有其他問題,因為代碼在此之外無法正常工作。 這個問題潛入的原因是因為你的泛型類型沒有類型安全。

例如:

@Override
public void insert(AnyType obj) { // <- AnyType here
    MyDoubleNode<AnyType> newNode = new MyDoubleNode<>(); // <- generic here
    newNode.data = obj;
    if (contains(obj) == false){
        if (head.prev == null){
            head.data = newNode;  // <- this does not compile now
        }
       ... etc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM