Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.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
在第n个位置插入节点 node*insertnth(struct node_q*list,int ndex,struct node_q*qtemp){//temp是要插入ndex后面的节点 结构节点_q*temp; 结构节点_q*curr; int i; 临时=列表; 电流=温度; 对于(i=0;ipnext; } 电流=温度; temp=temp->pnext; curr->pnext=NULL; curr->pnext=qtemp; qtemp->pnext=温度; 退货清单; }_C_Linked List - Fatal编程技术网

在第n个位置插入节点 node*insertnth(struct node_q*list,int ndex,struct node_q*qtemp){//temp是要插入ndex后面的节点 结构节点_q*temp; 结构节点_q*curr; int i; 临时=列表; 电流=温度; 对于(i=0;ipnext; } 电流=温度; temp=temp->pnext; curr->pnext=NULL; curr->pnext=qtemp; qtemp->pnext=温度; 退货清单; }

在第n个位置插入节点 node*insertnth(struct node_q*list,int ndex,struct node_q*qtemp){//temp是要插入ndex后面的节点 结构节点_q*temp; 结构节点_q*curr; int i; 临时=列表; 电流=温度; 对于(i=0;ipnext; } 电流=温度; temp=temp->pnext; curr->pnext=NULL; curr->pnext=qtemp; qtemp->pnext=温度; 退货清单; },c,linked-list,C,Linked List,我不知道它为什么会崩溃。这个函数应该在索引的后面或索引之后插入节点temp,并将其重新连接到列表。结构的所有指针在作为参数传递之前都设置为null,但它已经有节点的列表除外 node* insertnth(struct node_q* list,int ndex,struct node_q* qtemp){//temp is the node to be inserted at the back of ndex struct node_q* temp; struct node_q

我不知道它为什么会崩溃。这个函数应该在索引的后面或索引之后插入节点temp,并将其重新连接到列表。结构的所有指针在作为参数传递之前都设置为null,但它已经有节点的列表除外

node* insertnth(struct node_q* list,int ndex,struct node_q* qtemp){//temp is the node to be inserted at the back of ndex
    struct node_q* temp;
    struct node_q* curr;
    int i;
    temp = list;
    curr = temp;
    for(i=0;i<=ndex;i++){
        if(temp!=NULL)
        temp = temp->pnext;
    }
        curr = temp;
        temp = temp->pnext;
        curr->pnext = NULL;
        curr->pnext = qtemp;
        qtemp->pnext = temp;

        return list;
}
即使在这段代码中,您也可以访问
temp->pnext
,因此它不应为空

curr = temp;
        temp = temp->pnext;
        curr->pnext = NULL;
        curr->pnext = qtemp;
        qtemp->pnext = temp;
如果从1开始索引,最好更改循环条件,将节点插入索引位置

    if(temp!=NULL)
    {
    curr = temp;
    temp = temp->pnext;
    curr->pnext = NULL;
    curr->pnext = qtemp;
    qtemp->pnext = temp;
    }
    else
    {
       printf("wrong index");
    }

<代码> >(i=0;i)如果您为NDEX提供的值大于或等于(当前列表中的节点数)- 1,您将尝试解引用空指针并得到一个分段错误。考虑一个列表,其中有3个节点,NDEX值为函数的2输入:

列表-->节点0-->节点1-->节点2-->空

  • 启动条件:
    • 温度=节点0
    • curr=node 0
    • ndex=2
  • for循环的第一次迭代:
    • i=0,下一个为0(节点1)
  • 第二次迭代:
    • i=1,下一个为1(节点2)
  • 第三次迭代:
    • i=2,下一个为2(空)
  • 循环后操作:
    • 分配电流=温度(空)
    • 分配temp=temp->next(取消引用空指针并崩溃)
因此,您的代码确实存在两个问题:

  • 插入未发生在正确的位置,因为您混合了索引约定。输入到函数的索引值和for循环中的条件值的作用就像列表从1索引一样,但是for循环的开始条件的作用就像列表从0索引一样
  • 代码不会在尝试插入之前检查迭代是否已到达列表的末尾

如果你真的想在正常意义上插入
n
第四个位置,你就错了一个。在
n
位置插入通常意味着新项目在插入后位于
n
第四个位置。在C中,
n
从零开始。因此,在
0
第四个位置插入将新项目置于头部在
L
th插入,其中
L
是原始列表长度,将其放在最后

即使你修正了你的一个接一个的错误,代码也是混乱而难看的

如果您考虑将两个指针沿列表向下推进:一个“lead”和“trail”指针,那么这个问题会变得更容易。trail以NULL开头,lead指向列表头。将这对指针向前推进
n

使用一个漂亮的
for
循环习惯用法,可以迭代引导和跟踪指针:

for(i=0;i<ndex;i++){
另一种情况是,
trail
仍然指向NULL。这意味着
n
为零。在这种情况下,上面的代码将不起作用。第二行将失败。在此,您只需将新节点设为新列表头:

new_node->next = lead;
trail->next = new_node;
return list;
我会让你把这些碎片拼在一起。你应该得到一些小而漂亮的东西。

Public void Insert(键入object,int k,bool flag)
new_node->next = lead; 
return new_node;
{ Flag=false; ListNode当前=头; bool-found=false; while((current.next!=null)&&(found==false)) { if(current.data==obect) 发现=真;否则 当前=当前。下一步; } 如果(找到) System.out.println(“列表中已存在的对象”);else { 如果(k>n) { ListNode newNode=新ListNode(); newNode.data=object;newNode.next=null; current.next=newNode;tail=newNode } 否则,如果(k
list-->节点0-->节点1-->节点2-->为空
启动条件:
温度=节点0
curr=node 0
ndex=2
for循环的第一次迭代:
i=0,下一个为0(节点1)
第二次迭代:
i=1,下一个为1(节点2)
第三次迭代:
i=2,下一个为2(空)
循环后操作:
分配电流=温度(空)
分配temp=temp->next(取消引用空指针并崩溃)
public void InsertAtNth(整数数据,整数位置){
NodeS newNode=新节点(数据);
节点温度=头1;
节点prev=null;
if(head1==null){
head1=新节点;
}
否则如果(位置==0){
newNode.next=head1;
head1=新节点;
}
否则{
对于(int i=0;i
如果您有任何疑问,请点击
new_node->next = lead; 
return new_node;
Public void Insert (Type object, int k, bool flag) 
{ 
      Flag = false; 
      ListNode<Type> current = head; 
      bool found = false; 

       while ((current.next != null)&&(found == false)) 
     { 
    if(current.data == obiect) 
        found = true;   else 
        current = current.next; 
     } 
    If (found) 
            System.out.println(“The object already in the List”);    else 
        { 
            if (k > n) 
    { 
        ListNode<Type> newNode = new ListNode<Type>(); 
        newNode.data = object;          newNode.next = null; 
        current.next = newNode;         tail =newNode 
              } 
    else if(k <= 1) 
    { 
        ListNode<Type> newNode = new ListNode<Type>(); 
        newNode.data = object; 
        newNode.next = head.next;       head =newNode 

    } 
    else  
    { 
        int i = 1; 
        current = head; 
        while((I == k)&&(current.next != null)) 
        { 
            current = current.next; 
            i++; 
        } `enter code here`
list-->node 0-->node 1-->node 2-->NULL

Starting condition:
temp = node 0
curr = node 0
ndex = 2
First iteration of the for loop:
i = 0, 0 <= 2
temp != NULL, assign temp = node 0->next (node 1)
Second iteration:
i = 1, 1 <= 2
temp != NULL, assign temp = node 1->next (node 2)
Third iteration:
i = 2, 2 <= 2
temp != NULL, assign temp = node 2->next (NULL)
Post-loop operations:
assign curr = temp (NULL)
assign temp = temp->next (dereferences NULL pointer and crashes)
public void InsertAtNth(int data, int position){
            NodeS newNode = new NodeS(data);
            NodeS temp = head1;
            NodeS prev = null;
            if(head1 == null ){
                head1 = newNode;
            }
            else if(position == 0){
                  newNode.next = head1;
                  head1 = newNode;
              }

            else{
                for(int i = 0;i < position; i++){
                    prev = temp;
                    temp = temp.next;
                }
                prev.next = newNode;
                newNode.next = temp;

            }
        }