繁体   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