循环双向链表addToHead并打印

Circular Doubly Linked List addToHead and print

这是我在java的第一个作业,当我运行它时,它一直打印最后一个节点值,我不知道它是否理解它是循环的,不要不知道哪里出了问题!

public class CircularDoublyLinkedList {

public static void main(String argv[]) {
    CircularDoublyLinkedList List = new CircularDoublyLinkedList();
    List.printList();
    List.addToHead(10);
    List.printList();
    List.addToHead(20);
    List.addToHead(30);
    List.printList();

}

class Node {

    Object info;
    Node next, prev;

    Node() {
        info = null;
        next = null;
        prev = null;
    }


    Node(Object el, Node n, Node p) {
        info = el;
        next = n;
        prev = p;
    }
}

private Node head;

CircularDoublyLinkedList() {
    head = null;
}

public void addToHead(Object el) {
    Node tmp = new Node(el, null, null);
    if (head == null) {
        tmp.next = head;
        tmp.prev = head;
        head = tmp;
    }
    head.prev = tmp;
    head.prev.next = tmp;
    tmp.next = head;
    tmp.prev = head.prev;
    head = tmp;

}

public void printList() {

    Node tmp = head;
    if (head == null) {
        System.out.print("empty\n");
        return;
    }
    if (head.next == head) {
        System.out.print(head.info + " <-> " + tmp.info + " \n ");
        return;
    }

    System.out.print(head.info + " <-> ");
    tmp = head.next;
     while (tmp.next != head) {
        System.out.print(tmp.info+ " <-> ");
        tmp = tmp.next;
    }
}

这是输出 window:

empty
10 <-> 10 
30 <-> 20 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <-> 10 <->

问题是为什么不打印下面的内容?代码哪里有问题?

empty
10 <-> 10 
30 <-> 20 <-> 10 <-> 30

请帮忙,提前致谢:)

您的代码是多余的。我建议你清理它以了解它有什么问题。

首先,我不会使用Object类型。如果您希望 class 接受任何类型的数据,则可以使用 Java generics 使您的代码更清晰。此外,请注意 Node 是列表的一部分,因此不应与其他 "shared" 一起使用。您可以在 CircularLinkedList class.

中将其设为私有 class

我的建议如下:

public class CircularLinkedList<T> {
    private class Node<T> {
        private T element;
        private Node<T> next;
        private Node<T> previous;

        public Node(T element) {
            this.element = element;
            this.next = null;
            this.previous = null;
        }

        // getters and setters
    }
    private Node<T> head;

    public CircularLinkedList(){
        head = null;
    }
}

此时,在您的 CircularLinkedList class 方法中,您必须添加向头部添加元素的方法。尝试在一张纸上画出你想要的行为,当:1)列表是空的; 2)列表中只有一个节点; 3) 列表中有多个元素。结果应该如下:

public void addToHead(T element) {
    Node<T> newNode = new Node<T>(element);
    if (head == null) {
        // This is the first node you are adding to the list.
        head = newNode;
        head.setNext(head);
        head.setPrevious(head);
    }
    else {
        // Some nodes are already present in your list.

        // Going right.
        newNode.setNext(head);
        newNode.setPrevious(head.getPrevious());

        // Going left.
        newNode.getPrevious().setNext(newNode);
        newNode.getNext().setPrevious(newNode);
        head = newNode;
    }
}

要打印列表,请使用以下命令(同样在 CircularLinkedList 中):

    public void printList() {
        Node<T> temp = head;
        do {
            System.out.print(temp.getElement() + " ");
            temp = temp.getNext();
        } while (temp != head);
    }

以相反顺序打印列表(同样在 CircularLinkedList 中):

    public void printReverseList() {
        Node<T> temp = head;
        do {
            System.out.print(temp.getElement() + " ");
            temp = temp.getPrevious();
        } while (temp != head);
    }

如果您运行以下代码:

public static void main (String[] args) {
    // Notice the list declaration where T becomes an actual data type.
    CircularLinkedList<Integer> list = new CircularLinkedList<Integer>();

    list.addToHead(1);
    list.printList();
    System.out.println();
    list.printReverseList();
    System.out.println();

    list.addToHead(2);
    list.addToHead(3);
    list.printList();
    System.out.println();
    list.printReverseList();
}

您将获得此输出:

1 

1 

3 2 1 

3 1 2 

尽量保持代码的简洁和可读性。