本文介绍了C-嵌套的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个学生的链接列表,每个学生都有一个链接的成绩列表,但是我在访问学生的链接列表中访问成绩的链接列表时遇到了麻烦.
I'm trying to create a linked list of students, each with a linked list of grades, but I'm having trouble accessing the linked list of grades inside the linked list of students.
typedef struct student_data_struct{
char student[MAX];
struct grades_list_struct *gradeP;
} student_Data;
typedef struct student_list_struct{
student_Data studentData;
struct student_list_struct *next;
} StudentNode;
typedef struct grades_list_struct{
int grade;
struct grades_list_struct *next;
} GradeNode;
GradeNode *insertGrade(int grade, GradeNode *head){
GradeNode *newNode=NULL;
newNode=(GradeNode*)calloc(1, sizeof(GradeNode));
if(head!=NULL){
newNode->grade=grade;
newNode->next=head;
return newNode;
} else {
newNode->grade=grade;
newNode->next=NULL;
return newNode;
}
}
StudentNode *insertStudent(char studentName[MAX], int studentGrade, StudentNode *head){
StudentNode *newNode=NULL;
newNode=(StudentNode*)calloc(1, sizeof(StudentNode));
newNode->studentData->gradeP=(GradeNode*)calloc(1, sizeof(GradeNode));
if (head==NULL){
strcpy(newNode->studentData.student, studentName);
newNode->next=NULL;
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
return newNode;
} else {
strcpy(newNode->student, studentName);
newNode->gradeP->grade=studentGrade;
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
return newNode;
}
}
当我尝试将内存分配给成绩指针时,
When I try to allocate memory to the grade pointer,
newNode->studentData->gradeP=(GradeNode*)calloc(1, sizeof(GradeNode));
我得到了错误:
error: invalid type argument of '->' (have 'student_Data' {aka 'struct student_data_struct'})
同样,当我尝试为学生评分时,
As well, when I try to insert a grade for a student,
newNode->studentData->gradeP=insertGrade(studentGrade, newNode->studentData->gradeP);
我得到了错误:
error: invalid type argument of '->' (have 'student_Data' {aka 'struct student_data_struct'})
任何帮助将不胜感激.
推荐答案
您正在使用指针符号访问struct成员.尝试按如下所示编写:-
You are accessing a struct member with pointer symbol. Try writing as given below:-
newNode->studentData.gradeP=(GradeNode*)calloc(1, sizeof(GradeNode));
newNode->studentData.gradeP=insertGrade(studentGrade, newNode->studentData.gradeP);
这篇关于C-嵌套的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!