我刚刚从头创建了一个链表,到目前为止我已经创建了两个方法来将新元素插入到列表中。一种方法将元素放在列表的开头和列表中,另一种方法将元素放在列表的后面。我现在正在尝试创建第三种方法,其中我尝试放入列表中的元素将根据元素的值放置。因此,如果我想要插入的元素的值小于列表中的第一个元素,但是低于列表中的第二个元素,我希望该方法自动将新元素放在这两个元素之间。
理论上听起来很简单,但我现在意识到执行起来要比我想象的要困难得多。我似乎无法停止获取NullPointerExceptions和其他错误,所以此时我完全空白,不知道接下来该做什么。
Anywas,这是代码:
import java.util.*;
public class OrdnetLenkeliste<T extends Comparable<T>> extends Lenkeliste<T>{
public void insert(T element){
Node node = new Node(element);
if(super.isEmpty()){
node.next = head;
head = node;
numberOfNodes++;
} else {
Node currentNode = head;
Node previous = null;
while(currentNode != null){
if(currentNode.data.compareTo(node.data) < 0){
currentNode = currentNode.next;
} else if (currentNode.data.compareTo(node.data) > 0 && currentNode.previous.data.compareTo(node.data) < 0){
node = currentNode.previous;
currentNode.previous = node.previous;
currentNode = currentNode.next;
numberOfNodes++;
} else if(currrentNode.data.compareTo(node.data) == 0){
currentNode = currentNode.next;
currentNode.previous = node;
numberOfNodes++;
}
}
}
}
}
变量中可能存在一些拼写错误,但这只是因为我翻译了变量名以使代码更具可读性。
答案 0 :(得分:0)
您的代码中存在一些错误,并且您已经使它变得比它需要的更复杂。
首先,确实只有两种情况:新节点小于当前节点,在这种情况下新节点插入当前节点之前,或者它大于或等于当前节点,因此需要被插入列表中的某个地方。
所以这个想法非常简单:
loop until the new node's value is less than the current node's value
insert the new node before the current node
唯一的特殊情况是列表为空或新节点小于第一个节点。在这两种情况下,新节点都成为列表的头部。
实施更加冗长,但并不那么复杂。
public void insert(T element)
{
Node node = new Node(element);
Node currentNode = head;
Node previous = null;
// loop to find the insertion point.
while (currentNode != null && node.Data.compareTo(currentNode.data) >= 0)
{
previous = currentNode;
currentNode = currentNode.next;
}
// Insert the item between the previous node and the current node.
// This node's next pointer references the current node.
node.next = currentNode;
if (previous == null)
{
// Either the list is empty,
// or the new node is smaller than the head.
// This node becomes the head.
head = node;
}
else
{
// otherwise, the previous node's next pointer
// references the current node.
previous.next = node;
}
++numberOfNodes;
}
答案 1 :(得分:0)
在链接列表中插入新元素似乎有些错误。
while(currentNode != null){
if(currentNode.data.compareTo(node.data) < 0){
currentNode = currentNode.next;
}
else if (currentNode.data.compareTo(node.data) > 0 && currentNode.previous == null){
currentNode.previous = node;
head = node;
}
else if (currentNode.data.compareTo(node.data) > 0 && currentNode.previous.data.compareTo(node.data) < 0){
currentNode.previous.next = node; // set the next for previous node of current node
node.previous = currentNode.previous; // set previous and next of node
node.next = currentNode;
currentNode.previous = node; // set the previous of current node
numberOfNodes++;
} else if(currrentNode.data.compareTo(node.data) == 0){
// I am not sure what you want to do here, if you want to insert the node before current node, then do the same as above
currentNode.previous.next = node; // set the next for previous node of current node
node.previous = currentNode.previous; // set previous and next of node
node.next = currentNode;
currentNode.previous = node; // set the previous of current node
numberOfNodes++;
}
}