在链表中的特定节点之后插入
Inserting after a particular node in a linkedlist
这是我实现单向链表的完整 Java 源代码。我看过很多教程,他们一直在谈论在开始时插入一个节点。因此,我决定在我的代码中添加一个方法 insertAfterNode(int y)
,我可以在其中在特定节点之后的节点内添加数据。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datastructures;
/**
*
* @author Administrator
*/
public class Link {
int data;
Link next;
public Link(int d) {
this.data = d;
}
public void display() {
System.out.println(data);
}
public static class LinkedList {
Link first;
public void insertItemFirst(int x){
Link newLink = new Link(x);
newLink.next = first;
first = newLink;
}
public Link deleteFirst() {
Link temp = first;
first = first.next;
return temp;
}
public void displayList() {
Link current = first;
while (current != null) {
current.display();
current = current.next;
}
}
// START Of METHOD
public void insertAfterNode(int y) {
// Considering LinkedList is Sorted
Link newNode = new Link(y);
Link current = first;
while (current != null) {
while (current.data < y) {
current = current.next;
newNode.next = current;
current = newNode;
}
}
}
//END Of METHOD
}
public static void main(String[] args) {
LinkedList addelements = new LinkedList();
addelements.insertItemFirst(20);
addelements.insertItemFirst(30);
addelements.insertItemFirst(40);
addelements.insertItemFirst(50);
addelements.insertAfterNode(44);
addelements.displayList();
System.out.println("After Calling Deletion Method Once ");
addelements.deleteFirst();
addelements.displayList();
}
}
上面的代码在 Netbeans 中保持 运行,我不得不停止构建以退出它。我相信我的方法实现有问题。请让我知道我的以下方法有什么问题:
public void insertAfterNode(int y) {
// Considering LinkedList is Sorted
Link newNode = new Link(y);
Link current = first;
while (current != null) {
while (current.data < y) {
current = current.next;
newNode.next = current;
current = newNode;
}
}
}
没有上述方法,代码运行正常。
仅当 y
值大于 current.data
时,current
Link 引用才会更改。例如,如果 fist.data
是 10,而 y
是 5,您的代码将永远不会终止。
您必须修改您的 while(current != null)
周期。不要使用内部 while
.
Link newNode = new Link(y);
// "first" requires a special treatment, as we need to replace the "first" value
if(first.data > y){
// insert the new Link as first element
newNode.next = first;
first = newNode;
} else {
// we need the previous Link, because previous.next will be set to the newNode
Link previous = first;
Link current = first.next;
while (current != null) {
if(current.data < y) {
previous = current;
current = current.next;
} else {
// we insert newNode between previous and current
// btw. what should happen if current.data == y?
previous.next = newNode;
newNode.next = current;
break; // we are done, quit the cycle
}
}
// if the newNode.next is null at this point, means we have not inserted it yet.
// insert it as the last element in the list. previous is pointing at it.
if(newNode.next == null){
previous.next = newNode;
}
} // end of else
P.S。我希望它有效,我还没有测试代码:)
这是我实现单向链表的完整 Java 源代码。我看过很多教程,他们一直在谈论在开始时插入一个节点。因此,我决定在我的代码中添加一个方法 insertAfterNode(int y)
,我可以在其中在特定节点之后的节点内添加数据。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datastructures;
/**
*
* @author Administrator
*/
public class Link {
int data;
Link next;
public Link(int d) {
this.data = d;
}
public void display() {
System.out.println(data);
}
public static class LinkedList {
Link first;
public void insertItemFirst(int x){
Link newLink = new Link(x);
newLink.next = first;
first = newLink;
}
public Link deleteFirst() {
Link temp = first;
first = first.next;
return temp;
}
public void displayList() {
Link current = first;
while (current != null) {
current.display();
current = current.next;
}
}
// START Of METHOD
public void insertAfterNode(int y) {
// Considering LinkedList is Sorted
Link newNode = new Link(y);
Link current = first;
while (current != null) {
while (current.data < y) {
current = current.next;
newNode.next = current;
current = newNode;
}
}
}
//END Of METHOD
}
public static void main(String[] args) {
LinkedList addelements = new LinkedList();
addelements.insertItemFirst(20);
addelements.insertItemFirst(30);
addelements.insertItemFirst(40);
addelements.insertItemFirst(50);
addelements.insertAfterNode(44);
addelements.displayList();
System.out.println("After Calling Deletion Method Once ");
addelements.deleteFirst();
addelements.displayList();
}
}
上面的代码在 Netbeans 中保持 运行,我不得不停止构建以退出它。我相信我的方法实现有问题。请让我知道我的以下方法有什么问题:
public void insertAfterNode(int y) {
// Considering LinkedList is Sorted
Link newNode = new Link(y);
Link current = first;
while (current != null) {
while (current.data < y) {
current = current.next;
newNode.next = current;
current = newNode;
}
}
}
没有上述方法,代码运行正常。
仅当 y
值大于 current.data
时,current
Link 引用才会更改。例如,如果 fist.data
是 10,而 y
是 5,您的代码将永远不会终止。
您必须修改您的 while(current != null)
周期。不要使用内部 while
.
Link newNode = new Link(y);
// "first" requires a special treatment, as we need to replace the "first" value
if(first.data > y){
// insert the new Link as first element
newNode.next = first;
first = newNode;
} else {
// we need the previous Link, because previous.next will be set to the newNode
Link previous = first;
Link current = first.next;
while (current != null) {
if(current.data < y) {
previous = current;
current = current.next;
} else {
// we insert newNode between previous and current
// btw. what should happen if current.data == y?
previous.next = newNode;
newNode.next = current;
break; // we are done, quit the cycle
}
}
// if the newNode.next is null at this point, means we have not inserted it yet.
// insert it as the last element in the list. previous is pointing at it.
if(newNode.next == null){
previous.next = newNode;
}
} // end of else
P.S。我希望它有效,我还没有测试代码:)