java - 双向链表的测试问题

标签 java object doubly-linked-list

我编写了以下代码作为双向链表:

节点类:

 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) {

    }
}

接口(interface)类:

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() 的文档

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

关于java - 双向链表的测试问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42188057/

相关文章:

drupal - OO 和编写 Drupal 模块

java - 延迟会干扰我的计时逻辑吗?

创建本地和实例对象时出现 java StackOverflowError

java - 当在类属性中给出禁用时,如何在 Selenium 中查找元素是否被禁用

c++ - 从工作线程访问对象

c - 如何删除双链表中所有出现的特定字符?

java - 在 scala 中处理 csv

javascript - Angular 无法读取数组中的对象

c - 双链表出错,找不到 '*'之前缺失的属性

java - 从单链表扩展创建双链表 - 获取空指针