回 帖 发 新 帖 刷新版面

主题:关于建立和打印链表的小问题,困惑……

我想建立一个简单链表代码如下:
//基本结构:
struct Student
{
        char* pName;
        int cj[3];
        struct Student* next;
};

//建立链表:
void creatStruct(struct Student* head)
{
    struct Student* curr=(struct Student*)malloc(sizeof(struct Student));

        int i=0;

        clrscr();
        printf("\nPlease input Name:");
        scanf("%s",head->pName);
        printf("\nPlease input this student's grade(3 courses.Use SPACE to      separate):");
        for(;i<3;++i)
        scanf("%d",&(head->cj[i]));
        head->next=NULL;
        curr=head;

        while(0!=strcmp(curr->pName,"stop"))
       {
              struct Student* nextStu=(struct Student*)malloc(sizeof(struct Student));
                printf("\nPlease input Name:");
                scanf("%s",nextStu->pName);
                printf("\nPlease input this student's grade(3 courses.Use SPACE to separate):");
        for(i=0;i<3;++i)
            scanf("%d",&(nextStu->cj[i]));

        curr->next=nextStu;
        curr=nextStu;
        free(nextStu);
        }

    free(curr);


}


//输出:
void print(struct Student* head)
{
    struct Student* p=(struct Student*)malloc(sizeof(struct Student));
    p=head;
    while(0!=strcmp(p->pName,"stop"))
        {
                int i=0;
                printf("\n*************");
        printf("\nName:%s",p->pName);
                for(i=0;i<3;++i)
            printf("\nThe grade of this student is:%d",p->cj[i]);
        p=p->next;
        }
        printf("\n");
    free(p);
}


//主程序:
int main()
{
        struct Student* head=(struct Student*)malloc(sizeof(struct Student));
        creatStruct(head);
        print(head);
        free(head);
        getch();
        return 0;
}

很奇怪,单步跟踪的时候建立的过程没有问题,但是当输出时只能输出第一STUDENT的信息,剩下的就不正常了,且是个死循环,不停的输出同一组无效的数据。
(以输入stop为结束。)
实在搞不懂是哪里出了问题,还请高手指点。

回复列表 (共3个回复)

沙发

我知道你的问题出在哪里了
        creatStruct(head);
        print(head);
这两句话本意是建立链表后输出,2个head是同一个节点,但是你这样写 print(head);中的head并没有给他赋初值啊,因为这两个head都是局部变量,creatStruct(head);运行完了以后,head就释放掉了,print(head);这个head就是一个不知道指在那里的乱指针。
这样该:
把creatStruct(head);的返回类型改为struct Student *creatStruct(head);在该函数末尾加上return(head);再main函数中把 creatStruct(head);改为head=creatStruct(head);这样就应该可以了,你试试看吧

板凳

我觉得不尽然,这是我的一个改版,同样是用的传递参数,返回值是用来显示出错与否的.
/************************************************************/
/***********return 1 for success*****************************/
/***********return 0 for stop   *****************************/
/***********return -1 for error *****************************/
int creatStu(struct Student* head)
{
    int i;
    struct Student* prev=(struct Student*)malloc(sizeof(struct Student));
    struct Student* nextStu=(struct Student*)malloc(sizeof(struct Student));

    clrscr();
    printf("\nPlease input the student's name:");
    scanf("%s",head->sName);

    if(!strcmp(head->sName,"stop"))
    {
        head->next=NULL;
        return 0;
    }

    printf("\nPlease input this student's grades(3 courses Use SPACE to separate):");
    for (i=0;i<3;++i)
        scanf("%d",&(head->cj[i]));
    head->next=NULL;
    prev=head;

    do
    {
        nextStu=(struct Student*)malloc(sizeof(struct Student));
        printf("\nPlease input the student's name:");
        scanf("%s",nextStu->sName);

        if(!strcmp(nextStu->sName,"stop"))
        {
            nextStu->next=NULL;
            prev->next=nextStu;
            break;
        }

        printf("\nPlease input this student's course:");
        for (i=0;i<3;++i)
            scanf("%d",&nextStu->cj[i]);
        nextStu->next=NULL;
        prev->next=nextStu;
        prev=nextStu;

    }while(strcmp(prev->sName,"stop"));

    free(nextStu);
    free(prev);

    if(head)
        return 1;
    else
        return -1;
}
我的理解是在输入循环里,以前的版本是在循环里分配一个nextStu空间,又在循环里把它free掉了,个人觉得,这样子会把刚指向的curr这个指针指向的内容给释放掉,造成链表的各个项之间无法相连,从而在输出时只能输出head节点的内容.我的以前的版本是可以输出head的内容的,只是后面的全部为未知内容,且后面的空间里pName项又不是"stop",造成了死循环的发生.
不过还是感谢你给予帮助,其实一开始我也曾想过你说的问题,可是想来我的head是在main里分配的空间,即使在函数返回的时候把参数的空间释放了,也不会影响到head吧,否则如何解释能正确输出head节点内容呢?^_^

3 楼

有道理,理解理解

我来回复

您尚未登录,请登录后再回复。点此登录或注册