按字母顺序将节点插入双向链表时出现问题
Problem when alphabetically inserting a node into a doubly linked list
我正在尝试为双向链表编写插入方法,当我将节点放入列表时,它会按字母顺序插入。这个想法是我将使用 curnode 遍历列表,如果 newNode 出现在 curnode 之前,我将简单地将 newNode 放在 curnode 之前。到目前为止,我编写的代码适用于将 1 个节点插入到列表中,但我在第二部分遇到问题,需要检查顺序并在之前放置。我应该如何更改程序才能使其正常工作?使用我现在只有 1 个元素(头)的代码将被插入。
void insert(String x){
node curnode = head;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
break;
}
}
}
}
您的实施中缺少一些东西。将其与此工作解决方案进行比较:
void insert(String x){
node curnode = head;
node lastnode = null;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
if(temp != null) {
temp.next = newNode;
} else {
// If newnode gets inserted in the head
head = newNode;
}
break;
}
lastnode = curnode;
curnode = curnode.next;
}
if (curnode == null) {
// insert to the last
lastnode.next = newNode;
newNode.prev = lastnode;
tail = newNode;
}
}
}
我正在尝试为双向链表编写插入方法,当我将节点放入列表时,它会按字母顺序插入。这个想法是我将使用 curnode 遍历列表,如果 newNode 出现在 curnode 之前,我将简单地将 newNode 放在 curnode 之前。到目前为止,我编写的代码适用于将 1 个节点插入到列表中,但我在第二部分遇到问题,需要检查顺序并在之前放置。我应该如何更改程序才能使其正常工作?使用我现在只有 1 个元素(头)的代码将被插入。
void insert(String x){
node curnode = head;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
break;
}
}
}
}
您的实施中缺少一些东西。将其与此工作解决方案进行比较:
void insert(String x){
node curnode = head;
node lastnode = null;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
if(temp != null) {
temp.next = newNode;
} else {
// If newnode gets inserted in the head
head = newNode;
}
break;
}
lastnode = curnode;
curnode = curnode.next;
}
if (curnode == null) {
// insert to the last
lastnode.next = newNode;
newNode.prev = lastnode;
tail = newNode;
}
}
}