| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 289 人关注过本帖
标题:求看看这个链表有哪里不妥
只看楼主 加入收藏
fyt10060
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
求看看这个链表有哪里不妥
这个函数可以运行,但是其他的函数调用不了这个函数里提到的stulist
stu* inputInfo(stu * stulist ){
    int  i , stu_num;
    stu * pO =NULL, * pN =NULL ;
    printf("The number of the students that your want to input\n");
    scanf("%d" , &stu_num);
    while(stu_num>20||stu_num<=0){ /*控制输入信息的学生个数*/
        printf("The system can at most hold 20 student, please reinput\n");
        stu_num=GetInteger();
    }
    for(i=1; i<=stu_num ; i++){
        pN = (stu * )malloc(sizeof(struct stu));
        if (stulist==NULL) {/*判断是否是第一次输入*/
            stulist=pN;         /*定义头指针*/
        }
        else {
            pO->next=pN;      /*使其链接到下一个结构体*/
        }
        pN->next=NULL;  /*每一个结构体都可能是最后一个,所以将现在的结构体中指向下一个结构体的指针定为空指针*/
        printf("Please input the student's id\n");
        scanf("%d" , &pN->stu_id);
        while(uniqueNumber(stulist,pN->stu_id)==1){
            printf("The ID you put in is already there,please reinput\n");
            strcpy(pN->stu_id,GetInteger());
        }
        printf("Please input the student's name\n");
        scanf("%s" ,& pN->name);
        printf("Please input the student's score\n");
        scanf("%d" , &pN->score);
        while(checkscore(pN->score)==1){
            printf("The Score you put in is out of range,please keep it from 0 to 100\n");
            scanf("%d" , &pN->score);
        }
        pO=pN;

    }
          return stulist;
}
搜索更多相关主题的帖子: hold please system number 
2012-12-21 22:27
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:20 
stu是用typedef申明的吗?
 pN = (stu * )malloc(sizeof(struct stu));
那这里就不用在struct了,因为stu已经包含了这个信息
pN = (stu *)malloc(sizeof(stu));
要(stu)括起来因为sizeof是优先级
还有for循环那里的中括号打的有很多问题,
先修改修改吧,还有没有就不知道了,毕竟只有这一部分代码

I have not failed completely
2012-12-21 22:39
fyt10060
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-21
收藏
得分:0 
哦,我刚刚自己做出来了,是调用函数时的错误,但是发现其他函数能调用了,但只能调用这个函数里输入的第一个整体的结构体信息
下面是一个其他函数的例子,就是比如用上面的函数输入了有两个结构体的链表,但是在下面这个output只会显示出第一个,为什么啊?

void outputInfo(stu * stulist){
    stu * pN;
    pN=stulist;
    if(pN!=NULL){
        do{
            printf("ID :%d , Name: %s , Score : %d .\n" , pN->stu_id , pN->name , pN->score);
        pN=pN->next;
        }while (pN->next!=NULL);
    }
    else if(pN==NULL)  {
        printf("The system don't have any information. Please input some first\n");
    }
}
2012-12-21 22:44
fyt10060
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-12-21
收藏
得分:0 
再次补充下,估计是我自己的错误了,后面的只会显示比输入的信息个数少1个的信息,就是输入3个学生信息,返回的只有2个,不过我还是看不出是哪的问题,帮忙看下,谢谢
2012-12-21 22:53
快速回复:求看看这个链表有哪里不妥
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012223 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved