圆形双链表AddToHead并打印

时间:2015-11-01 11:26:29

标签: java linked-list

这是我在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;
    }
}

这是输出窗口:

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

问题是为什么它不打印以下内容?代码中的问题在哪里?

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

请提前帮助,谢谢:)

1 个答案:

答案 0 :(得分:1)

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

首先,我不会使用Object类型。如果您希望您的班级接受任何类型的数据,您可以使用Java generics使您的代码更清晰。此外,请注意Node是列表的一部分,因此不应与其他人“共享”。您可以在CircularLinkedList类中将其设为私有类。

我的建议如下:

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类的方法中,您必须添加向头部添加元素的方法。尝试在一张纸上绘制您想要的行为,时间: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 

尽量保持代码清晰可读。

相关问题