【问题标题】:Adding to the end of a linked list not working添加到链表的末尾不起作用
【发布时间】: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;

【问题讨论】:

标签: c linked-list


【解决方案1】:

我看到的一件事是,当您在 for() 循环中初始化指针时,您正在跳过链表的第一个成员。如果您的列表为空怎么办?这不会在末尾添加一个节点。

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2013-12-21
    相关资源
    最近更新 更多