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 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'和'false'需要在c中定义。否则使用 1 和 0 代替。 return 0 将不起作用,因为该函数被定义为 void。从声明中删除“void”或使用“return;”

关于c - 在 C 中的列表末尾添加新结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556228/

相关文章:

c++ - 使用线程时 Linux 事件轮询不起作用

c - : &data[0] vs. 数据之间的差异

我可以在 Xcode 中使用 C 中的快速帮助语法吗?

c - 以双引号开头的行在 C 中是什么意思?

c++ - 大输入上释放对象的校验和不正确

c - 将多个头文件链接到c文件

c - 静态/常量类型 *Const 差异

我可以在 C 中使用带有返回值的 if 语句作为函数参数吗?

c - 除以 1/n 总是返回 0.0

C、printf循环和换行