| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 476 人关注过本帖
标题:动态链表问题
只看楼主 加入收藏
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
动态链表问题
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{long num;
char name[20];
float score[3];
struct student *next;};

void main()                      //主函数
{struct student *stu;
struct student *shuru();
void chuli(struct student *p);
void shuchu(struct student *p);
stu=shuru();
chuli(stu);
shuchu(stu);}

struct student *shuru()          //输入函数
{ struct student *head,*p1,*p2;  int n;
   p1=p2=(struct student*)malloc(LEN);
   scanf("%ld,%s,%f,%f,%f",&p1->num,p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
   head=NULL;
   for(n=1;n<3;n++)
   {
      if(n==1) head=p1;
      else  p2->next=p1;
      p2=p1;
      p1=(struct student*)malloc(LEN);
      scanf("%ld,%s,%f,%f,%f",&p1->num,p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
    }
   p2->next=NULL;    return(head);
}

struct student *p1;          //全局变量

void chuli(struct student *p)   //处理函数
{float a=0.0,b;
for(p;p->next!=NULL;p=p->next)
{b=(p->score[0]+p->score[1]+p->score[2])/3.0;
if(a<b) a=b,p1=p;}}

void shuchu(struct student *p)         //输出函数
{for(p;p->next!=NULL;p=p->next)
{printf("%s的平均分是",p->name);
printf("%f\n",(p->score[0]+p->score[1]+p->score[2])/3.0);}
printf("最高分的学生是%s\n",p1->name);}


检测通过,但运行错误,为什么啊????我想了很久的了,哎,求救啊
搜索更多相关主题的帖子: include 动态 
2011-12-21 22:34
lijy520
Rank: 2
等 级:论坛游民
帖 子:119
专家分:42
注 册:2007-8-8
收藏
得分:4 
scanf("%ld,%s,%f,%f,%f",&p1->num,p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
scanf 中 %s 接受输入时 以 空格/回车为结束
所经你输入时 会发生错误
建议 将此scanf 中 ',' 号去掉运行试试

2011-12-21 23:34
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
收藏
得分:0 
还是不行
2011-12-22 13:20
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:6 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)

struct student
{
    long            num;
    char            name[20];
    float            score[3];
    struct student    *next;
};

void main()                      //主函数
{
    struct student *stu;
    struct student *shuru ();
    void chuli  (struct student *p);
    void shuchu (struct student *p);
    stu = shuru ();
    chuli (stu);
    shuchu (stu);
}

struct student *shuru()          //输入函数
{

    struct student    *head, *p1 = NULL, *p2 = NULL; 

    int                n;

   


    head = NULL;
    for (n = 1; n <= 3; n++)
    {
        p1 = (struct student*) malloc (LEN);
        scanf ("%ld%s%f%f%f", &p1 -> num, p1 -> name,
            &p1 -> score[0], &p1 -> score[1], &p1 -> score[2]);
        if (n == 1)

            head = p1;
        else 

            p2 -> next = p1;
        p2 = p1;
    }
    p2 -> next = NULL;   

    return (head);
}

struct student *p1;          //全局变量

void chuli (struct student *p)   //处理函数
{
    float a = 0.0, b;
    for    (; p; p = p -> next)
    {
        b = (p -> score[0] + p -> score[1] + p -> score[2]) / 3.0;
        if (a < b)
            a = b, p1 = p;
    }
}

void shuchu (struct student *p)         //输出函数
{
    for (; p ; p = p -> next)
    {
        printf ("%s的平均分是", p -> name);
        printf ("%f\n", (p -> score[0] + p -> score[1] + p -> score[2]) / 3.0);
    }
    printf ("最高分的学生是%s\n", p1 -> name);
}
scanf输入的问题就像2楼说的那样,要把逗号去掉才行。
然后就是逻辑错误了,shuru()中的for循环会导致最后一个数据存不进链表中。处理和输出函数中的判断不是判断next是否为NULL,而是当前指针变量是否为NULL,不然会少处理和少输出一个数。



冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-12-22 17:42
daming1
Rank: 1
来 自:开平
等 级:新手上路
帖 子:21
专家分:0
注 册:2011-11-26
收藏
得分:0 
行了,万分感谢!
2011-12-22 18:31
快速回复:动态链表问题
数据加载中...
 
   



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

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