Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在喜欢的列表中插入任意节点?_Java_Data Structures_Linked List - Fatal编程技术网

Java 如何在喜欢的列表中插入任意节点?

Java 如何在喜欢的列表中插入任意节点?,java,data-structures,linked-list,Java,Data Structures,Linked List,我将要实现一个函数,它可以在链表中插入任意节点。下面的代码适用于在列表的第一个位置插入节点,但不适用于在另一个节点之后放置节点。我真的搞不懂这段代码到底是怎么回事。当我追踪代码的时候,我也找不到我的错误。请不要禁止我,帮我解决这个问题。提前谢谢。 类节点来了: public class Node { Object Element; Node Link; public Node() { this(null,null); } public Node(Object Element, Nod

我将要实现一个函数,它可以在链表中插入任意节点。下面的代码适用于在列表的第一个位置插入节点,但不适用于在另一个节点之后放置节点。我真的搞不懂这段代码到底是怎么回事。当我追踪代码的时候,我也找不到我的错误。请不要禁止我,帮我解决这个问题。提前谢谢。 类节点来了:

public class Node {
Object Element;
Node Link;

public Node() {
    this(null,null);
}

public Node(Object Element, Node Link) {
    this.Element = Element;
    this.Link = Link;
}

}
班级名单:

 public class List {
Node FirstNode;
Scanner UserInfo = new Scanner(System.in);
Scanner UserInput = new Scanner(System.in);
public List() {
    FirstNode = null;
}
public void InsertArbitrary() {
int Location = UserInput.nextInt(); // Location of new node
if (Location == 1) {
    Object Element = UserInfo.nextLine();
    FirstNode = new Node(Element, FirstNode); // locates a New Node At First
} else {
    Object Element = UserInfo.nextLine(); // Content of new node
    Node CurrentNode ; // for searching in the list 
    CurrentNode = FirstNode;
    for (int i = 1; i <= Location - 1; i++)
        CurrentNode = CurrentNode.Link;
    Node NewNode = new Node (Element , CurrentNode);
}
}
}
公共类列表{
节点第一节点;
扫描仪用户信息=新扫描仪(System.in);
扫描仪用户输入=新扫描仪(System.in);
公开名单(){
FirstNode=null;
}
public void InsertArbitrary(){
int Location=UserInput.nextInt();//新节点的位置
如果(位置==1){
Object元素=UserInfo.nextLine();
FirstNode=新节点(元素,FirstNode);//首先查找新节点
}否则{
Object Element=UserInfo.nextLine();//新节点的内容
Node CurrentNode;//用于在列表中搜索
CurrentNode=FirstNode;

对于(int i=1;i一旦您迭代到要插入的位置,您就可以正确地创建一个新节点并将其链接分配到下一个元素。但是您没有做的是更新上一个链接以指向您的新节点,因此列表的尾部不再可以从头节点访问

您必须这样做(未经测试):

Node-FirstNode;
整数长度=0;
公开名单(){
FirstNode=null;
}
public void InsertArbitrary(int位置,对象元素){
如果(位置==1 | |长度==0){
FirstNode=新节点(元素,FirstNode);//首先查找新节点
长度++;
}否则{
Node CurrentNode;//用于在列表中搜索
CurrentNode=FirstNode;

对于(int i=1;i而言,问题在于您没有更新对下一个节点的引用

在不提供代码(因为这是家庭作业)的情况下,如果您希望在a之后插入一个新节点X,请使用伪代码:

x.next = a.next
a.next = x

因此,链片段从
a->b
变为
a->x->b

,没有足够的上下文代码来理解它的作用。(如果您想要更快的答案,请不要违反命名约定规则。)@青蛙,我加了更多details@ErFunJava变量和方法名称以小写字母开头。您的命名约定使所有内容看起来都像类。@m0skit0节点构造函数的第二个参数是对下一个节点的引用。因此保留列表的其余部分,因为新节点的下一个指针引用列表的旧头d成为新的列表头。它不起作用。我在线程“main”java.lang.NullPointerException中得到了这个错误“Exception”,这个错误属于“Node NewNode=new Node(Element,CurrentNode.Link);”当插入到链接列表中的任意位置时,必须注意列表的当前长度,以避免越界。例如,插入到空列表的位置5是没有意义的。我已经更新了我的代码段来表达这一点。当然,您还应该测试代码,以确定位置索引是否为c管理得当。
x.next = a.next
a.next = x