请教一个结构体和链表的问题,请大家帮帮忙看看,谢谢了,

xiaofeng3385 2005-04-10 12:36:19
问题本身可能就有问题,大家不要笑我,我的想法是::

定义一个学生的结构体数组,
typedef struct student{
int id;
int age;
char name[20];
struct kc *head;
}stu[i];

里面的 *head 指向一个链表,stu[i]都有自己的链表 ,里面存入课程信息,
即head指向下面这个结构体

typedef struct kc{
int keid;
int score;
char kename[10];
struct kc *next;
};

请问可以输入i的直,或学生id,然后可以查找到自己相应的链表么,输出链表中的内容?
假如就象 str[i].head 这样,不要笑我,我真的不太懂呀,
...全文
206 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
virm 2005-04-16
  • 打赏
  • 举报
回复
to 月之海
由于在网吧,没办法测试我的代码,谬误见笑了
呵呵,多多切磋
llf_hust 2005-04-10
  • 打赏
  • 举报
回复
可以
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
可以根据每个结构体中的学生id 来查找么?
llf_hust 2005-04-10
  • 打赏
  • 举报
回复
有 stu[i].head 这种用法,不过你在查找的时候要根据某个关键字才能找到对应的指针呀
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
我看到书中有stu[i].id stu[i].name这样的用法,
不知道 有没有 stu[i].head 这种.指针的用法呀,
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
上面问题解决了,
呵呵,可是最后的查询时不能查到正确的结果呀,

printf("Find StudentID of Number ?\n");
scanf("%d",&id);
for(i=0;i<N;++i)
{
if(stu[i].id == id)break;
}
if(i!=N)
print(stu[i].head);

为什么那?
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
月之海:

我调试时,在main中的 struct kc *head=create();
出现错误: Expression syntax in funtion main
然后我在上面声明了 struct kc *head; 然后在刚才的位置改为 head=create();

这样编译可以通过了,不过运行时出现了问题是:folating point formats not linked,
Abnormal program termination;

请问这是怎么回事呀???帮帮忙/
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
真的很感谢你门,我在去调试一下,
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
程序的运行用TC2.0,3.0或VC好象都有区别的,有时换个编译器出现很多毛病,也浪费了我不少时间呀。

问题能解决都靠大家的帮助,特别谢谢月之海不厌其烦的帮助,
小弟谢过了。小弟在沈阳,以后来这一定找我呀,呵呵
zengwujun 2005-04-10
  • 打赏
  • 举报
回复
也是你昨天的程序,你参考一下:
#include<string.h>
#include<stdio.h>
#include <malloc.h>
#define null 0
#define len sizeof(struct kc)

struct kc
{
int keid;
char kename[20];
float score;
struct kc *next;
};

#define N 3
struct student
{
int id;
int age;
char name[20];
struct kc *head;
};
student stu[N];

int n;
struct kc *create(void)
{
struct kc *p1,*p2;
struct kc *head;
n=0;
p1=p2=(struct kc *)malloc(len);
scanf("%d,%f,%s",&p1->keid,&p1->score,&p1->kename);
head=null;
while(p1->keid !=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct kc*)malloc(len);
scanf("%d,%f,%s\n",&p1->keid,&p1->score,&p1->kename);
};
p2->next=null;
return(head);
}

void print(struct kc *head)
{
struct kc *p;
printf("fenshushi::");
p=head;
if(head !=null)
do
{
printf("%d,%s,%f\n",p->keid,p->kename,p->score);
p=p->next;
}while(p!=null);
}

void main()
{
int id;
int age;
char name[20];
for(int i=0;i<N;++i)
{
printf("The Number of %d Student-->\n",i);
printf(" Enter CourseID,Score,CourseName:\n");
struct kc *head = create();
printf(" Enter StudentID,AGE and Name:\n");
scanf("%d,%d,%s",&id,&age,&name);
stu[i].id=id;
stu[i].age=age;
strcpy(stu[i].name,name);
stu[i].head=head;
printf("\n");
}

printf("Find StudentID of Number ?\n");
scanf("%d",&id);
for(i=0;i<N;++i)
{
if(stu[i].id == id)break;
}
if(i!=N)
print(stu[i].head);
}
xjp6688 2005-04-10
  • 打赏
  • 举报
回复
www.rayoko.com
www.vcok.com
上面有类似,你看看
sunandmoon1314 2005-04-10
  • 打赏
  • 举报
回复
不会出错拉
zengwujun 2005-04-10
  • 打赏
  • 举报
回复
上面那些create,print都是他自己写的,我只写了main(),他这个题都问了几百分了.
我是vc6,运行我的程序没有问题,输入查询都可以呢.
输入依次是这样的:
1,90,maths
2,80,english
0

1,19,zeng

1,80,maths
2,70,english
0

2,18,wang

1,70,maths
2,60,english
0

3,17,li

2//查询2号学生

输出也没问题
virm 2005-04-10
  • 打赏
  • 举报
回复
原来的
printf(" Enter CourseID,Score,CourseName:\n");
struct kc *head = create();
应该合并到下面
stu[i].head=head;
因为现有学生,然后有学生的成绩
printf(" Enter CourseID,Score,CourseName:\n");
stu[i].head = create();

virm 2005-04-10
  • 打赏
  • 举报
回复
本函数只是对 zengwujun(月之海) 兄台的一点修正和美化
很佩服zengwujun(月之海)兄的基本功,交个朋友

struct kc *create(void)
{
struct kc *head,*pLast,*pNew;
head = pLast = null;
while( true )
{
pNew = (struct kc*)malloc( sizeof(struct kc));
/* 更改:pNew->kename已经为指针,原来再加上引用符是错误的 */
scanf("%d,%f,%s\n",&pNew->keid,&pNew->score, pNew->kename);
if( 0 == pNew->keid ) /* 输入课程id为0 表示结束课程输入 */
{
free( pNew ); /* 修正:最后一次无效输入应该释放 */
pLastKc->next = null;
return head; /* 函数唯一出口,返回第一门课程 */
}
if(null == pLast){ /* 处理链表为空的情况*/
head = pLastKc = pNew ;
continue;
}
pLastKc->next = pNew ; /*追加 新课程 */
pLastKc = pNew; /*新课程设为最后一个课程*/
}
}
virm 2005-04-10
  • 打赏
  • 举报
回复
struct kc *create(void)
{
struct kc *head,*pLast,*pNew;
head = pLast = null;
while( true )
{
pNew = (struct kc*)malloc( sizeof(struct kc));
/* pNew->kename已经为指针,原来的再加上引用是错误的 */
scanf("%d,%f,%s\n",&pNew->keid,&pNew->score, pNew->kename);
if( 0 == pNew->keid )
{
free( pNew );
pLaskKc->next = null;
return head; /* 函数唯一出口 */
}
if(null == pLast){
head = pLaskKc = pNew ;
continue; }
}
pLaskKc->next = pNew ;
pLastKc = pNew;
}
}
llf_hust 2005-04-10
  • 打赏
  • 举报
回复
不会
xiaofeng3385 2005-04-10
  • 打赏
  • 举报
回复
我每次输入完学生的信息后,在输入链表的信息,
这样输入3次后,每个学生都有自己的链表,我只是动态的建立链表,
这样子根据学生ID查到的链表信息会不会出错哦???

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧