Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
C 在链表中插入:Bug_C_Algorithm_Insert_Linked List - Fatal编程技术网

C 在链表中插入:Bug

C 在链表中插入:Bug,c,algorithm,insert,linked-list,C,Algorithm,Insert,Linked List,在下面的代码中:插入函数不能插入多于2个节点 #include <stdio.h> #include <stdlib.h> struct node { int val; struct node * next; }; struct node * insertl(struct node * head,int value) { if(head==NULL) { struct node * temp= malloc(sizeof(struct node))

在下面的代码中:插入函数不能插入多于2个节点

#include <stdio.h>
#include <stdlib.h>

struct node
{
int val;
struct node * next;
};

struct node * insertl(struct node * head,int value)
{
   if(head==NULL)
  {
     struct node * temp= malloc(sizeof(struct node));
     temp->val = value;
     temp->next=NULL;
     head=temp; printf("%d",value);
     return head;
 }
 if(head->next==NULL)
 {
    struct node * temp= malloc(sizeof(struct node));
    temp->val = value;
    temp->next=NULL;
    head->next = temp; printf("%d",value);
    return head;
 }
 struct node *head1=head;
 while(!head1->next)
 {
    head1=head1->next;
    printf("%d",head1->val);
 }

 struct node * temp= malloc(sizeof(struct node));
 temp->val = value;
 temp->next=NULL;
 printf("%d",temp->val-90);
 head1->next = temp;
 return head;
 }

  void print(struct node *head)
{     printf("\n");
   struct node * temp = head;
   while(temp!=NULL)
  {
    printf("%d\t",temp->val);
    temp=temp->next;
   }

 }

int main()
{   struct node * h =NULL;
    h=insertl(h,1);
    h=insertl(h,4);
    h=insertl(h,1);
    h=insertl(h,4);
    print(h);
 }
#包括
#包括
结构节点
{
int-val;
结构节点*下一步;
};
结构节点*insertl(结构节点*head,int值)
{
if(head==NULL)
{
结构节点*temp=malloc(sizeof(结构节点));
温度->值=值;
temp->next=NULL;
压头=温度;打印F(“%d”,值);
回流头;
}
if(head->next==NULL)
{
结构节点*temp=malloc(sizeof(结构节点));
温度->值=值;
temp->next=NULL;
head->next=temp;printf(“%d”,值);
回流头;
}
结构节点*head1=head;
而(!head1->next)
{
head1=head1->next;
printf(“%d”,标题1->val);
}
结构节点*temp=malloc(sizeof(结构节点));
温度->值=值;
temp->next=NULL;
打印F(“%d”,temp->val-90);
head1->next=温度;
回流头;
}
无效打印(结构节点*头)
{printf(“\n”);
结构节点*温度=头部;
while(temp!=NULL)
{
printf(“%d\t”,temp->val);
温度=温度->下一步;
}
}
int main()
{struct node*h=NULL;
h=插入式(h,1);
h=插入式(h,4);
h=插入式(h,1);
h=插入式(h,4);
印刷品(h);
}
我在两者之间使用printf语句只是为了检查代码哪里出错。 输出显示:1,4。为什么代码不正确

struct node *head1=head;
while(!head1->next)
      ^

我想你是想在列表的“末尾”添加内容,你的意思是
while(head1->next)

非常感谢你这么快的回复。我觉得很糟糕,因为我太粗心了