C 困惑于如何添加到另一个结构中的链表结构

C 困惑于如何添加到另一个结构中的链表结构,c,struct,linked-list,C,Struct,Linked List,我想将节点添加到另一个结构中的结构的链表中 我的代码: struct Courses{ char *courseName; int creditValue; Courses *next; };Courses; struct Student{ char *name; int age; Courses *list; //First course (node) }Student; 现

我想将节点添加到另一个结构中的结构的链表中

我的代码:

 struct Courses{
        char *courseName;
        int creditValue;
        Courses *next;
    };Courses;

struct Student{
        char *name;
        int age;
        Courses *list;  //First course (node)
    }Student;
现在我不知道如何通过student结构为需要添加到列表中的各种课程(节点)添加课程节点


有人能告诉我怎么做吗

这是一个将一个节点添加到列表头部的函数:

 struct Courses{
     char *courseName;
     int creditValue;
     struct Courses *next;
  };

  struct Student{
      char *name;
      int age;
      struct Courses *list;
  };

  void addNodeAtHead( struct Student *student, struct Courses *newNode )
  {
      newNode->next = student->list; // successor of newNode is head of list
      student->list = newNode;       // new head of list is newNode
  } 

这是一个将一个节点添加到尾部列表的函数:

  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
      }
  }
注意:如果创建新列表,则必须使用
NULL
初始化
struct Student
的结构元素
list

void addCourse( struct Student *s, const char* courseName, int creditValue )
{
    struct Courses *course = malloc( sizeof( Courses ) ); 
    course->courseName = malloc( strlen(courseName)+1 );
    strcpy( course->courseName, courseName );
    course->creditValue = creditValue;
    addNodeAtTail( student, course );
}

struct Student *student = malloc( sizeof( Student) );
student->list = NULL;
....

addCourse( student, "course", 666 );
如何打印列表:

void printList( struct Student *student )
{
      struct Courses *temp = student->list;
      while( temp != NULL )
      {
          printf( "courseName: %s, creditValue %d\n", courseName, creditValue );
          temp = temp->next;
      }
}

代码中的一些错误和前面的答案。我想你应该定义一个叫做Courses的结构,你只需要

struct Courses{
    char *courseName;
    int creditValue;
    Courses *next;
};
如果不希望为变量的每个实例键入struct Course。加上

typedef struct Courses Courses;
学生结构也一样

struct Student{
  char *name;
  int age;
  Courses *list;  //First course (node)
};
首先,应该使用用户定义的函数初始化struct变量,该函数给出struct student变量的名称、年龄并将指针设置为NULL

比如说

void init(struct Student* s, char * name, int age ){
  s->age = age;
  s->name = malloc(strlen(name) + 1);
  strcpy(s->name,name);
  s->list = NULL;
 }
如果要定义“添加课程”函数 下面是一个例子

 void add_course(struct Student*s, char* course_name,int credit){
      struct Courses* c = malloc(sizeof (struct Courses) );
      c->courseName = course_name;
      c->creditValue = credit;
      c->next = NULL;

      if (s->list == NULL)
            s->list = c;
      else{
            while (s->list->next != NULL)
                  s->list->next = s->list->next->next;

            s->list->next = c;
       }
 }
然后编写一个函数来清理malloc()分配的资源

我想你是C的新手。如果你想运行我的代码,请包括必要的库,如

#include <stdio.h> 
#include<stdlib.h> 
#include<string.h>
#包括
#包括
#包括

请再问一些问题……

新课程需要放在某个特殊的位置(比如说最后一个、第一个……)@dubafek列表中还没有填写任何内容。因此,第一门课程将是第一个节点,之后的每一门课程都将连接到上一门课程。列表是否在Student结构中并不重要。添加节点的代码与添加任何其他链表的代码相同。我希望始终添加到尾部。因此,如果列表为空,我将添加到尾部,但由于列表为空,节点将是第一个节点,之后的每个节点都将添加到尾部。如果我想采用这个程序,我会一直使用你的第二个函数“AddNodeAtail”,对吗?我永远不用用“AddNodeHead”对吧?即使列表是空的,并且我想添加第一个节点,但如果我使用“AddNodeAtail”,它仍然可以正常工作?对您的代码有一个疑问。当您执行“newNode->next=NULL”时,是否正确?它前面不应该有“学生->列表”吗?因为您正在通过学生结构访问课程结构?谢谢您的帮助。我还有一个问题,有没有办法从学生结构打印“课程*列表”中的所有节点?我知道如何直接从课程结构打印,但我想知道如何从学生结构打印整个列表。