这是我在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("emptyn");
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
请帮忙,提前感谢:)
您的代码是多余的。我建议你把它清理一下,了解它出了什么问题。
首先,我不会使用Object
类型。如果您希望您的类接受任何类型的数据,您可以使用Java泛型来使代码更干净。此外,请注意,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
尽量保持代码的整洁和可读性。