在C中的列表末尾添加新结构

在C中的列表末尾添加新结构,c,C,我想在列表的末尾插入结构,但无法编译代码。 下面是我为insert()函数编写的一些伪代码。代码的其余部分应该保持不变 (check it succeeded) set the data for the new person if the current list is empty (i.e. NULL) do the same as insert_start i.e. set the new person's "next" link to point to the curre

我想在列表的末尾插入结构,但无法编译代码。 下面是我为insert()函数编写的一些伪代码。代码的其余部分应该保持不变

 (check it succeeded)
 set the data for the new person
 if the current list is empty (i.e. NULL)
   do the same as insert_start i.e.
   set the new person's "next" link to point to the current list (i.e. NULL)
   return the (start of the) new list i.e. a pointer to the new person
 otherwise
   use a loop to find the last item in the list
     (i.e. the one which has a "next" link of NULL)
   set the "next" link of this item to point to the new person
     so the new person becomes the last item in the list
     (i.e. the new person should have a "next" link of NULL)
       return the (start of the) list
编译错误:

error: 'true' undeclared (first use in this function)
insert_end.c:47:13: note: each undeclared identifier is reported only once for each function it    appears in
insert_end.c:57:3: warning: 'return' with a value, in function returning void
make: *** [insert_end] Error 1




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


/* these arrays are just used to give the parameters to 'insert',
 to create the 'people' array */
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
      "Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};


/* declare your struct for a person here */
typedef struct Record{
  char *name;
  int age;
  struct Record *next;
}  Record;

//set the head pointer at the start of the list
Record *headptr = NULL;

static void insert (Record *p, char *s, int n) {

  /* allocate heap space for a record */
  Record *ptr =(Record*) malloc(sizeof(Record));

  if(ptr == NULL){  
    abort();
    printf("memory allocation fail"); 
    exit(1);  
  }else{
    printf("memory allocation to person  - %s - \n", s);      
  }

  ptr->name=s;
  ptr->age=n;
  ptr->next = NULL;

  if(p->next==NULL)
  {
     p->next= ptr;

  }else{
      Record *current = p;
      while(true){

    if(current->next ==NULL)
{
  current->next = ptr;
  break;
}
current= current->next;
  }
  }
  return 0;

}  

int main(int argc, char **argv) {

      /* declare the people array here */
  Record *p;
  headptr = NULL;

   //insert the members and age into the unusage array. 
  for (int i=0; i < 7; i++) {
    insert (p, names[i], ages[i]);
    /* do not dereference the pointer */
  }

  /* print out a line before printing the names and ages */
  printf("\n");

  //set the pointer at the start of the list 
  p = headptr;

  /* print the people array here*/
  for (int i=0; i < 7; i++, p = p->next) {
    printf("The name is: %s, the age is:%i\n", p->name, p->age);
  }


  /* This is the third loop for call free to release the memory allocated by malloc */
  /* the free()function deallocate the space pointed by ptr. */
  for(int i=0; i<7;i++){
    free(p);
  }


}
错误:“true”未声明(首次在此函数中使用)
insert_end.c:47:13:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次
在返回void的函数中插入_end.c:57:3:警告:带有值的“return”
make:**[insert_end]错误1
#包括
#包括
/*这些数组仅用于为“insert”提供参数,
创建“人员”数组的步骤*/
字符名[][10]={“西蒙”、“苏西”、“阿尔弗雷德”、“奇普”、“约翰”、“蒂姆”,
“哈丽特”};
整数年龄[7]={22,24,106,6,18,32,24};
/*在此处为某人声明您的结构*/
类型定义结构记录{
字符*名称;
智力年龄;
结构记录*下一步;
}记录;
//将头指针设置在列表的开头
记录*headptr=NULL;
静态空插入(记录*p,字符*s,整数n){
/*为记录分配堆空间*/
记录*ptr=(记录*)malloc(sizeof(记录));
如果(ptr==NULL){
中止();
printf(“内存分配失败”);
出口(1);
}否则{
printf(“分配给人员的内存-%s-\n”,s);
}
ptr->name=s;
ptr->age=n;
ptr->next=NULL;
如果(p->next==NULL)
{
p->next=ptr;
}否则{
记录*电流=p;
while(true){
如果(当前->下一步==NULL)
{
当前->下一步=ptr;
打破
}
当前=当前->下一步;
}
}
返回0;
}  
int main(int argc,字符**argv){
/*在这里声明人员数组*/
记录*p;
headptr=NULL;
//将成员和年龄插入未使用的数组。
对于(int i=0;i<7;i++){
插入(p,姓名[i],年龄[i]);
/*不要取消对指针的引用*/
}
/*在打印姓名和年龄之前先打印一行*/
printf(“\n”);
//将指针设置在列表的开头
p=水头;
/*在此处打印人员数组*/
对于(inti=0;i<7;i++,p=p->next){
printf(“名称为:%s,年龄为:%i\n”,p->name,p->age);
}
/*这是释放malloc分配的内存的callfree的第三个循环*/
/*函数的作用是:释放ptr指向的空间*/

对于(int i=0;i,需要在c中定义“真”和“假”。否则,使用1和0代替。 返回0将不起作用,因为函数定义为void。请从声明中删除“void”或使用“return;”

对于“未定义的true”,您应该
#包括
,除非您绑定到C89。