我将以下代码编写为双向链表:
节点类:
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());
}
}
如果我的代码有点荒谬,我道歉。我刚刚学习了泛型和链表,并没有完全理解它。当我运行我的测试代码时,我得到节点内存中的位置,而不是节点上的数据。我如何解决这个问题,以便打印我添加到列表中的数字?
答案 0 :(得分:1)
最佳答案是Object.toString()
的文档返回对象的字符串表示形式。通常,toString方法返回一个“文本表示”此对象的字符串。结果应该是一个简洁但信息丰富的表示,便于人们阅读。建议所有子类都覆盖此方法。
类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”和对象的哈希码的无符号十六进制表示组成。换句话说,此方法返回一个等于值的字符串:
getClass().getName() + '@' + Integer.toHexString(hashCode())
答案 1 :(得分:1)
真正的问题是您正在为数据分配节点。我看到的输出(我足以运行你的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