门外汉2008 发表于 2008-7-18 13:42

关于链表...

题目:22.N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。
问题一:这个题完全不会编.
问题二:链表是个啥东西,有啥用(希望某高手能介绍一下[tk28] ),动态分配内存是不是在做大程序是不是有一定优势
?
问题三:链表是不是C语言往深处发展的突破口,因为和数据结构和图论有关?

门外汉2008 发表于 2008-7-19 07:28

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:28

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:29

回复 3# 门外汉2008 的帖子

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:29

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:29

回复 5# 门外汉2008 的帖子

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:30

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:30

回复 7# 门外汉2008 的帖子

怎么没人看啊,自己把它顶起...

门外汉2008 发表于 2008-7-19 07:31

怎么没人看啊,自己把它顶起...[tk27]

门外汉2008 发表于 2008-7-19 07:31

回复 9# 门外汉2008 的帖子

[tk23] 怎么没人看啊,自己把它顶起...

chengli 发表于 2008-7-19 07:45

我来帮你编一个

struct student
{
    float score;
    struct student * next;
}
fun(struct student * h)
{
   float max=h->score;
   h=h->next;
   while(h!=NULL)
    {
       if(h->score>max) max=h->score;
    }
}
可以动态分配内存;是程序简洁
一般大程序都少不了链表

chengli 发表于 2008-7-19 12:08

忘了 返回 max了

float fun(struct student * h)
{
   float max=h->score;
   h=h->next;
   while(h!=NULL)
    {
       if(h->score>max) max=h->score;
    }
      return max;
}

门外汉2008 发表于 2008-7-20 09:03

回复 12# chengli 的帖子

那主函数应该怎么编写赋值啊,我这样编结果有12个error:
void main()
{
        struct stud_type
        {char name[20];
        long num;
        int age;
        char sex;
        float score;
        }*h;
        h=&student[0].score[0];
        struct stud_type student[3]={{"Wang Li",200701,18,'M',90.5},
        {"Zhang Fun",200702,19,'M',89.5},{"Li Ning",200703,'F',98}};
printf("%f\n",fun(h));
}

门外汉2008 发表于 2008-7-20 09:03

回复 13# missiyou 的帖子

那主函数应该怎么编写赋值啊,我这样编结果有12个error:
void main()
{
        struct stud_type
        {char name[20];
        long num;
        int age;
        char sex;
        float score;
        }*h;
        h=&student[0].score[0];
        struct stud_type student[3]={{"Wang Li",200701,18,'M',90.5},
        {"Zhang Fun",200702,19,'M',89.5},{"Li Ning",200703,'F',98}};
printf("%f\n",fun(h));
}

门外汉2008 发表于 2008-7-20 10:49

#13

又搞了一下主函数只有3个错了,但还是有错,各位高手帮忙啊:
#include<stdio.h>
struct student
{
float score;
struct student * next;
}
struct student *fun(struct student *h)
{
  struct student *p,*q;
  p=q=h;
while(p!=NULL)
{
  if(p->score<p->next->score)
   q=p->next;

p=p->next;

}
return q;
  
}
void main()
{
        struct student *h,a[3]={90.5,89.5,98};
h=&a[0];
printf("%f\n",fun(h));
}

子林 发表于 2008-7-20 11:14

你好 这个看看C语言书就OK了  特别是指针和链表这两章 好好看看

zhong0711101 发表于 2008-7-20 15:29

[em05]

门外汉2008 发表于 2008-7-20 15:55

回复 18# missiyou 的帖子

首先要谢谢你[tk12],但是你的程序好象还有些问题,它不论怎样总是只输出第3个数
好象起不到比较的作用...

[[it] 本帖最后由 门外汉2008 于 2008-7-20 16:04 编辑 [/it]]

missiyou 发表于 2008-7-20 16:25

呵呵,又开了一个玩笑,不是我故意的,5分钟解决
#include<stdio.h>
struct student
{
float score;
struct student * next;
};
struct student *fun(struct student *h)
{
  struct student *p,*q;
  p=q=h;
while(p ->next!=NULL)
{
  if(p->score < p->next->score)
   q=p->next;

p=p->next;

}
return q;
  
}
int main()
{
    struct student *h,a,b,c;
    a.score=80.5;
    h=(struct student *)malloc(sizeof(struct student));
h=&a;
b.score=55.5;
h->next=(struct student *)malloc(sizeof(struct student));
h->next=&b;
c.score=10.5;
h->next->next=(struct student *)malloc(sizeof(struct student));
h->next->next=&c;
h->next->next->next=NULL;
printf("%f\n",(fun(h))->score);
system("pause");
return 0;
}

门外汉2008 发表于 2008-7-20 18:29

回复 21# missiyou 的帖子

恩,这次好了,谢谢.
其实有句话想对你说..你的头像象个明星..胡彦斌..[tk02]

页: [1]

编程论坛