| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2017 人关注过本帖
标题:为什么程序运行只能输入一次,循环不能进行到底,求大神指教
只看楼主 加入收藏
F3268724562
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-7-30
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
为什么程序运行只能输入一次,循环不能进行到底,求大神指教
#include <stdio.h>
#include <stdlib.h>
typedef struct STUDENTINF
{
    char name[20];
    float score;
    struct STUDENTINF *next;
}stu;
//创建链表;
struct stu* creat(int n)
{
    int i;
    stu* head;
    stu* note;
    stu* end;
    head=(stu*)malloc(sizeof(stu));
    head=end=NULL;   
    for(i=1;i<n;i++)
    {
        note=(stu*)malloc(sizeof(stu));
        printf("请输入学生的姓名\n");
        scanf("%s",note->name);
        printf("请输入学生的成绩\n");
        scanf("%d",&note->score);
        end->next=note;
        end=note;
    }
    end->next=NULL;
    return head;     
}
//遍历链表;
void output(stu *head)
{
    stu* note;
    note=head;
    while(note->next!=NULL)
    {
        printf("姓名:%s\n",note->name);
        printf("成绩:%d\n",note->score);
        note=note->next;
    }
    return ;
}
int main()
{   
    struct stu* creat(int n);
    void output(stu *head);
    stu* head;
    int n;
    printf("请输入创建链表的个数\n");
    scanf("%d",&n);
    head=creat(n);
    output(head);
    return 0;
}
搜索更多相关主题的帖子: 输入 stu int head note 
2019-07-30 16:40
wufuzhang
Rank: 9Rank: 9Rank: 9
来 自:广州
等 级:贵宾
威 望:21
帖 子:206
专家分:1346
注 册:2017-8-9
收藏
得分:20 
回复 楼主 F3268724562
你的代码问题挺多的,我把修改的地方注释出来了,你仔细看看。

程序代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct STUDENTINF
{
    char name[20];
    float score;
    struct STUDENTINF *next;
}stu;
//创建链表;
struct STUDENTINF* creat(int n)             //这里要用结构体的名称,不能用别名 
{
    int i;
    stu* head;
    stu* note;
    stu* end;
    head=(stu*)malloc(sizeof(stu));
    head=end=NULL;    
    for(i=1;i<=n;i++)                        //i从1开始,那么i<=n才是循环了n次 
    {
        note=(stu*)malloc(sizeof(stu));
        printf("请输入学生的姓名\n");
        scanf("%s",note->name);
        printf("请输入学生的成绩\n");
        scanf("%f",&note->score);            //score是float类型,用格式符%f 
        
        if (i==1) head = note;                //如果第一次循环,要把值赋给head,不然head一直是NULL 
        else end->next = note;                //第二次及以后循环,把值赋给end 
        end = note;
    }
    end->next = NULL;
    return head;     
}
//遍历链表;
void output(stu *head)
{
    stu* note;
    note = head;
    while(note != NULL)                        //这里是判断note指针是否为NULL,不为NULL就输出该指针所指向的内容 
    {
        printf("姓名:%s\n",note->name);
        printf("成绩:%f\n",note->score);    //%f 
        note = note->next;
    }
    return ;
}
int main()
{   
    struct STUDENTINF* creat(int n);
    void output(stu *head);
    stu* head;
    int n;
    printf("请输入创建链表的个数\n");
    scanf("%d",&n);
    if (n<1) exit(0);                        //当输入的个数<1,退出程序 
    head=creat(n);
    output(head);
    return 0;
}

不经历千百遍的调试,怎能体会成功时那一刹那的喜悦。
2019-07-31 09:43
F3268724562
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-7-30
收藏
得分:0 
回复 2楼 wufuzhang
谢谢大神,我明白了。
2019-07-31 15:43
快速回复:为什么程序运行只能输入一次,循环不能进行到底,求大神指教
数据加载中...
 
   



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

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