【发布时间】:2018-11-30 03:45:25
【问题描述】:
当尝试将学生添加到我的列表末尾时,结果显示字母顺序错误。我已尝试更改函数的添加到结尾部分,但似乎看不出有什么问题。
这是我添加到结尾的方式
Student *prvPtr= headStudentList;
for(Student *curPtr = headStudentList->next; curPtr != NULL; curPtr = curPtr->next)
{
if (curPtr->next==NULL){
curPtr->next= newPtr; //newPtr
return headStudentList;
}
if(strcmp(curPtr->lastName,last)<0 ){ //change from first
if(strcmp(curPtr->firstName,first)<0 )
{
newPtr->next=curPtr->next;
curPtr->next =newPtr; //curPtr->next =newPtr
return headStudentList; //headStudentList
}
}
}
这是结构内部的内容
typedef struct _grade {
char name[4];
double value;
struct _grade *next;} Grade;
/////////////////////////////////////// //////////////////////////////////////////
typedef struct _student {
char *lastName;
char *firstName;
Grade *headGradeList;
struct _student *next;} Student;
【问题讨论】:
-
查看stackoverflow.com/questions/53380109/…了解链表的实现。
-
太短了
name(3-chars +'\0') 请提供A Minimal, Complete, and Verifiable Example (MCVE)。 -
姓名只是一个标识符,即 Q10,而不是实际学生姓名,学生姓名是 char lastName 和 char firstName
-
在管理链接列表插入或删除的代码中看到字符串比较是不常见的。另见How to create a Minimal, Complete, and Verifiable example。
标签: c linked-list