Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 如何在链表的第n个位置插入节点_Java_Data Structures_Linked List - Fatal编程技术网

Java 如何在链表的第n个位置插入节点

Java 如何在链表的第n个位置插入节点,java,data-structures,linked-list,Java,Data Structures,Linked List,我是编程初学者。我试图用java实现链表,我曾尝试编写函数在第n个位置插入元素,但它不能正常工作,不能在该位置之前显示数据。这对你们来说可能是一个愚蠢的问题或错误,但因为我是初学者,所以你们的回答会很有帮助,我们将不胜感激 提前谢谢 代码如下 class Node{ int data; Node next; Node(){ data=0; next=null; } } class LinkedList{ Node head; LinkedList(){ head=nul

我是编程初学者。我试图用java实现链表,我曾尝试编写函数在第n个位置插入元素,但它不能正常工作,不能在该位置之前显示数据。这对你们来说可能是一个愚蠢的问题或错误,但因为我是初学者,所以你们的回答会很有帮助,我们将不胜感激

提前谢谢

代码如下

class Node{
int data;
Node next;
Node(){
    data=0;
    next=null;
}
}

class LinkedList{
Node head;
LinkedList(){
    head=null;
}

   void pushB(int item){
       Node temp=new Node();
       temp.data=item;
       temp.next=null;
        if(head==null){
        head=temp;    
        }
        else{
            temp.next=head;
            head=temp;
        }
    }

    void pushnth(int item, int pos){

     Node cur=new Node();
     cur.data=item;
     cur.next=null;
     Node temp=head;
     int i=0;

     while(i<pos-1){
         temp=temp.next;
         i++;
     }
     cur.next=temp;
     head=cur;
    }

    void print(){
        if(head==null){
            System.out.println("List empty");
        }

        else{
            Node temp=head;
            while(temp!=null){
                System.out.println(temp.data);
                temp=temp.next;
            }
        }
    }
}


public class MyFirstJavaProgram {

public static void main(String []args) {
   System.out.println("Hello World");

   LinkedList l1=new LinkedList();

   l1.pushB(90);
   l1.pushB(80);
   l1.pushB(70);
   l1.pushB(60);
   l1.pushB(50);
   l1.pushB(30);
   l1.pushB(20);
   l1.pushB(10);
   l1.pushnth(40,4);
   l1.print();
}
} 
类节点{
int数据;
节点下一步;
节点(){
数据=0;
next=null;
}
}
类链接列表{
节点头;
LinkedList(){
head=null;
}
无效pushB(整数项){
节点温度=新节点();
温度数据=项目;
温度next=null;
if(head==null){
压头=温度;
}
否则{
下一个温度=水头;
压头=温度;
}
}
无效推力(内部项目,内部位置){
Node cur=新节点();
当前数据=项目;
cur.next=null;
节点温度=头;
int i=0;

而(i您的
pushnth
方法会更改列表的
标题,因此会丢弃新元素之前的所有元素

为了在列表的中间添加一个元素,您必须设置2个链接

新节点应指向下一个链接,您可以在此处执行此操作:

cur.next=temp;
temp
之前的节点应链接到新节点。这是您缺少的部分

像这样的方法应该会奏效:

void pushnth(int item, int pos){

    Node cur=new Node();
    cur.data=item;
    Node temp=head;
    int i=0;

    while(i<pos-2){ // note that I changed the end condition
        temp=temp.next;
        i++;
    }
    // the new node is placed between temp and temp.next
    cur.next = temp.next;
    temp.next = cur;
}
void pushnth(内部项目,内部位置){
Node cur=新节点();
当前数据=项目;
节点温度=头;
int i=0;
while(i
//请注意,这是正确的添加节点位置。
公共静态节点addAtPosition(节点头3,内部位置){
//添加节点的数据中包含0
节点NodeDatPosition=新节点(1000);
节点温度=头3;
如果(位置==0){
//添加节点的数据中包含1000
nodeadatposition.next=head3;//将addFront分配到head旁边
head3=nodeadatposition;//我们需要返回head,将head分配到前面。
}否则{
对于(int i=1;i
您必须区分新项目插入列表开头的情况:

void pushnth(int item, int pos){

     Node cur=new Node();
     cur.data=item;
     if (pos == 0) {
         cur.next = head;
         head = cur;
     } else {
         Node temp=head;
         for (int i=1; i<pos; ++i) {
            temp=temp.next;
         }
         cur.next = temp.next;
         temp.next = cur;
      }
    }
void pushnth(内部项目,内部位置){
Node cur=新节点();
当前数据=项目;
如果(位置==0){
cur.next=头部;
头=电流;
}否则{
节点温度=头;

for(int i=1;我非常感谢@Eran。我明白了。这里我们不是在修改头部,而是头部如何给我们修改列表。@Parvez如果您希望新节点成为第一个节点,您只能修改头部(在
pushB
中执行此操作)当你在中间添加一个元素时,你可以通过改变列表中的某个节点X的X.NEXT来修改列表。因此,这里我们甚至没有返回任何列表,所以它是如何给我们正确的输出,因为我们没有修改过头部,而在打印功能中,我们使用的是未修改的头部。@ PARVEZ即使头部没有被修改,你也改变了其中一个。原始列表中的节点指向新节点。这更改了列表。
void pushnth(int item, int pos){

     Node cur=new Node();
     cur.data=item;
     if (pos == 0) {
         cur.next = head;
         head = cur;
     } else {
         Node temp=head;
         for (int i=1; i<pos; ++i) {
            temp=temp.next;
         }
         cur.next = temp.next;
         temp.next = cur;
      }
    }