为什么我在这个双向链表中收到错误?
Why am I getting an error in this Doubly Linked List?
所以我一直在研究双向链表,我想在列表的头部插入一个值。
我们应该有 2 个案例:
- 空列表的情况。
- 非空列表的情况。
我认为我所做的是正确的,但我对结果仍然有疑问。
public class DoublyLinkedList {
class Element{
int data ;
Element next = null; //reference the next element
Element previous = null; //reference to the previous element
Element(int value){
data = value;
next = null;
previous = null;
}
}
private Element head = null; //reference to the head of the list
private Element rear = null; //reference to the rear of the list
private int length = 0;
public static final int NOT_FOUND = -1;
//getter return the number of items in the list
public int getLength() {
return length;
}
public DoublyLinkedList() {
this.head = null;
this.rear = null;
this.length = 0;
}
public DoublyLinkedList(DoublyLinkedList dList) {
this();
if(dList.isEmpty())
return;
Element cur = dList.head;
Element tmp = new Element(cur.data);
head = rear = tmp;
cur = cur.next;
length++;
while(cur != null) {
tmp = new Element(cur.data);
rear.next = tmp;
tmp.previous = rear;
rear = tmp;
cur = cur.next;
length++;
}
}
public boolean isEmpty() {
return length == 0;
}
public String toString() {
Element cur = this.head;
String str;
if (isEmpty())
str = "The list is empty";
else {
str = "|";
while (cur != null) {
str += cur.data + "|";
cur = cur.next;
}
System.out.println();
}
return str;
}
public void insertAtHead(int value) {
Element tmp = new Element(value);
// case on an empty list
if(this.isEmpty()) {
head = rear = tmp;
head.previous = null;
rear.next = null;
}else {
//case of a non-empty list
tmp.next = head;
head.previous = tmp;
tmp.previous = null;
head = tmp;
this.length++;
}
}
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
list.insertAtHead(1);
list.insertAtHead(2);
list.insertAtHead(3);
list.insertAtHead(4);
list.insertAtHead(5);
System.out.println(list);
list.insertBetween2Nodes(3);
System.out.println(list);
}
}
它总是给我这样的结果:
列表为空。
在您的 insertAtHead
函数中,当列表为空时,您不会增加列表长度。应该是:
public void insertAtHead(int value) {
Element tmp = new Element(value);
// case on an empty list
if(this.isEmpty()) {
head = rear = tmp;
head.previous = null;
rear.next = null;
} else {
//case of a non-empty list
tmp.next = head;
head.previous = tmp;
tmp.previous = null;
head = tmp;
}
// You always need to increment length
this.length++;
}
所以我一直在研究双向链表,我想在列表的头部插入一个值。 我们应该有 2 个案例:
- 空列表的情况。
- 非空列表的情况。
我认为我所做的是正确的,但我对结果仍然有疑问。
public class DoublyLinkedList {
class Element{
int data ;
Element next = null; //reference the next element
Element previous = null; //reference to the previous element
Element(int value){
data = value;
next = null;
previous = null;
}
}
private Element head = null; //reference to the head of the list
private Element rear = null; //reference to the rear of the list
private int length = 0;
public static final int NOT_FOUND = -1;
//getter return the number of items in the list
public int getLength() {
return length;
}
public DoublyLinkedList() {
this.head = null;
this.rear = null;
this.length = 0;
}
public DoublyLinkedList(DoublyLinkedList dList) {
this();
if(dList.isEmpty())
return;
Element cur = dList.head;
Element tmp = new Element(cur.data);
head = rear = tmp;
cur = cur.next;
length++;
while(cur != null) {
tmp = new Element(cur.data);
rear.next = tmp;
tmp.previous = rear;
rear = tmp;
cur = cur.next;
length++;
}
}
public boolean isEmpty() {
return length == 0;
}
public String toString() {
Element cur = this.head;
String str;
if (isEmpty())
str = "The list is empty";
else {
str = "|";
while (cur != null) {
str += cur.data + "|";
cur = cur.next;
}
System.out.println();
}
return str;
}
public void insertAtHead(int value) {
Element tmp = new Element(value);
// case on an empty list
if(this.isEmpty()) {
head = rear = tmp;
head.previous = null;
rear.next = null;
}else {
//case of a non-empty list
tmp.next = head;
head.previous = tmp;
tmp.previous = null;
head = tmp;
this.length++;
}
}
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
list.insertAtHead(1);
list.insertAtHead(2);
list.insertAtHead(3);
list.insertAtHead(4);
list.insertAtHead(5);
System.out.println(list);
list.insertBetween2Nodes(3);
System.out.println(list);
}
}
它总是给我这样的结果:
列表为空。
在您的 insertAtHead
函数中,当列表为空时,您不会增加列表长度。应该是:
public void insertAtHead(int value) {
Element tmp = new Element(value);
// case on an empty list
if(this.isEmpty()) {
head = rear = tmp;
head.previous = null;
rear.next = null;
} else {
//case of a non-empty list
tmp.next = head;
head.previous = tmp;
tmp.previous = null;
head = tmp;
}
// You always need to increment length
this.length++;
}