[英]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;
}
}
這是輸出窗口:
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
是列表的一部分,因此不應與其他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
嘗試使您的代碼盡可能干凈和可讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.