当前位置: 代码迷 >> C语言 >> 请大家帮我分析这个程序
  详细解决方案

请大家帮我分析这个程序

热度:935   发布时间:2007-12-27 21:54:03.0
请大家帮我分析这个程序
大家帮我来看一下这个程序:输入一些学生的信息(学号和分数),然后输出。
为什么我的程序在调试时没有查出错误,却运行不了呢?
先谢谢各位了。
#include"stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *nest;
}
main()
{
    struct student *head,*lest,*p;
    int n;
    n=0;
    head=NULL;
    p=(struct student*)malloc(LEN);
    printf("请输入学生的学号和分数<当学号为0时结束>\n");
    scanf("%ld%f",p->num,p->score);
    p->nest=NULL;
    while(p->num!=0)
    {
         ++n;
         if(n==1)
             head=p;
         else
             lest->nest=p;
         lest=p;
         p=(struct student*)malloc(LEN);
         scanf("%ld%f",p->num,p->score);
         p->nest=NULL;
    }
    printf("%s\t,%s\n","学号","分数");
    while(p!=NULL)
    {
            printf("%ld\t,%f",p->num,p->score);
            p=p->nest;
    }
}
搜索更多相关的解决方案: include  

----------------解决方案--------------------------------------------------------
忘记把表头指向head了
#include"stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    struct student *nest;
}
main()
{
    struct student *head,*lest,*p;
    int n;
    n=0;
    head=NULL;
    p=(struct student*)malloc(LEN);
    printf("请输入学生的学号和分数<当学号为0时结束>\n");
    scanf("%ld%f",p->num,p->score);
    p->nest=NULL;
    while(p->num!=0)
    {
         ++n;
         if(n==1)
             head=p;
         else
             lest->nest=p;
         lest=p;
         p=(struct student*)malloc(LEN);
         scanf("%ld%f",p->num,p->score);
         p->nest=NULL;
    }
    printf("%s\t,%s\n","学号","分数");
    while(head!=NULL)
    {
            printf("%ld\t,%f",head->num,head->score);
            head=head->nest;
    }
}
----------------解决方案--------------------------------------------------------
可是还是不行啊,请大家帮我分析一下吧,我最近才接触链表,不怎么熟啊,
----------------解决方案--------------------------------------------------------
  相关解决方案