我有一个排序的双向链表,其中第一个和最后一个元素为空。这意味着当我插入值a,b,c时。结果应如下所示:{null,a,b,c,null}
空的已排序的双向链表应如下所示:{null,null},其中第一个和最后一个元素始终为null。
问题是,当我在排序的双链表中插入数据时,数据没有正确排序,并且2个空值始终位于列表的末尾。我该如何解决?
这是我当前的插入方法:
public void addElement(String element) {
// new node which will be inserted in the list
Node newNode = new Node();
newNode.data = element;
// if the list is empty
if (size == 0) {
last = newNode;
newNode.next = first;
first = newNode;
size++;
} else {
Node current = first;
// if the element should be at the beginning of the list
if (current.data.compareTo(element) > 0) {
newNode.next = current;
newNode.previous = null;
current.previous = newNode;
first = newNode;
} else {
while (current != null) {
if (current.data.compareTo(element) <= 0) {
if (current.next == null) {
newNode.next = current.next;
newNode.previous = current;
current.next = newNode;
break;
}
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
}
size++;
}
}
答案 0 :(得分:1)
目前尚不清楚您在代码中正在做什么,因此我对其进行了一些修改并制作了更多的OO样式,因此如下:
moveright
输出:
class Node {
String data;
Node next, previous;
}
public class SortedDLL {
private Node first;
private Node last;
private int size = 0;
public SortedDLL() {
size = 0;
first = new Node();
last = new Node();
first.next = last;
last.previous = first;
}
public void addElement(String element) {
Node newNode = new Node();
newNode.data = element;
if (size == 0) {
first.next = newNode;
newNode.previous = first;
newNode.next = last;
last.previous = newNode;
} else {
Node node = first;
while (node.next.data != null && node.next.data.compareTo(newNode.data) < 0) {
node = node.next;
}
newNode.next = node.next;
node.next.previous = newNode;
node.next = newNode;
newNode.previous = node;
}
size++;
}
public void print() {
Node node = first;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.next;
}
}
public void printReverse() {
Node node = last;
while (node != null) {
System.out.print(node.data != null ? node.data + " " : "null ");
node = node.previous;
}
}
public static void main(String[] args) {
SortedDLL sortedDLL = new SortedDLL();
sortedDLL.addElement("c");
sortedDLL.addElement("a");
sortedDLL.addElement("b");
sortedDLL.addElement("c");
System.out.println("list: ");
sortedDLL.print();
System.out.println("\nlist reverse: ");
sortedDLL.printReverse();
}
答案 1 :(得分:0)
问题从size == 0的第一个电话开始
将第一个null推到末尾。第一个节点成为新节点。
然后,如果解决此问题,则将在该行获得空指针异常:
if (current.data.compareTo(element) > 0) {
因为当前将为null并且将没有数据。
您应该忽略第一个插入内容中的第一个空值,以及此后的每个插入物。
答案 2 :(得分:0)
取决于实现方式,我认为您只是在错误的地方做正确的事情。
while (current != null) {
if (current.next == null) {
newNode.next = null;
newNode.previous = current;
current.next = newNode;
break;
}
if (current.next.data.compareTo(element) > 0) {
newNode.next = current.next;
newNode.previous = current;
current.next.previous = newNode;
current.next = newNode;
break;
} else {
current = current.next;
}
}
不是检查当前选定的节点是否较小,而是需要检查之后的节点是否较大,因为这样您可以放置该节点。 并且需要在该比较之外检查current.next是否为null。