Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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 可以使用添加到列表末尾的相同链表函数添加到许多不同的链表中吗?_C_Struct_Linked List - Fatal编程技术网

C 可以使用添加到列表末尾的相同链表函数添加到许多不同的链表中吗?

C 可以使用添加到列表末尾的相同链表函数添加到许多不同的链表中吗?,c,struct,linked-list,C,Struct,Linked List,这是我的链表函数,用于将新节点添加到列表的末尾 void addNodeAtTail( struct Student *student, struct Courses *newNode ) { newNode->next = NULL; // sucessor of newNode is NULL, because newNode becomes tail of list if ( student->list == NULL )

这是我的链表函数,用于将新节点添加到列表的末尾

void addNodeAtTail( struct Student *student, struct Courses *newNode )
  {
      newNode->next = NULL;           // sucessor of newNode is NULL, because newNode becomes tail of list
      if ( student->list == NULL )
          student->list = newNode;    // if list is empty, newNode will be head of list
  else
  {
      struct Courses *temp = student->list;
      while( temp->next != NULL ) // search last element in list
          temp = temp->next;
      temp->next = newNode;       // successor of last node is newNode
  }
  }

是否可以使用相同的函数将节点添加到我可能拥有的不同链表中?或者我必须创建另一个函数,因为我们正在处理另一个结构?

如果您尝试添加的对象不是
struct Courses*newNode
,那么您将无法使用此函数添加它。如果你需要一个通用的链表,你就需要在传递
void*
时乱来,这很有趣。以下是一个合理的教程:


目前没有,因为您正在明确处理
结构课程的实例。
如果需要常规列表,请使用cons单元格:

struct list { 
   struct list* next;
   void* data;
}
此外,C中的列表可以嵌入到其他结构中。请参阅Linux内核中如何使用列表的示例。以下是文章中的一个片段:

struct my_cool_list{
    struct list_head list; /* kernel's list structure */
    int my_cool_data;
    void* my_cool_void;
};