简体   繁体   中英

Testing Issues with Doubly Linked Lists

I wrote the following code to be a doubly linked list:

Node class:

 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 Class:

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

Doubly Linked List Class:

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

Tester class:

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

I apologize if my code is a little nonsensical. I just learned generics and linked lists, and don't fully understand it. When I run my test code, I get the location in memory of the node, instead of the data on the node. How can I fix this, so that it prints the numbers I added to the list?

The best answer is the documentation of 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())
 

The real problem is you are assigning nodes to data. The output I saw (I was good enough to run your MCVE and not just guess) was MyDoubleNode@7f31245a but I knew you only printing .data values, that means a node ended up in the .data . Sure enough:

head.data = newNode;

This should possibly be:

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

You have other issues, because the code does not work beyond that. The reason that this issue sneaked in is because you do not have type safety around your generic type.

For example:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM