结构体之链表

网友投稿 585 2022-10-30

结构体之链表

结构体之链表

#include struct student { long num; float score; struct student *next;};void main(){ struct student a,b,c,*head; a.num = 10101; a.score = 89.5; b.num = 10103; b.score = 90.5; c.num = 10107; c.score = 85; head = &a; a.next = &b; b.next = &c; c.next = NULL; do { printf("%1d %5.1f\n", head->num, head->score); head = head->next; } while( head );}

答案

#include #include #include #definestruct student *creat(); void print(struct student *head);struct student { int num; float score; struct student *next;};int n;void main(){ struct student *stu; stu = creat(); print( stu ); printf("\n\n"); system("pause");}struct student *creat() { struct student *head; struct student *p1, *p2; p1 = p2 = (struct student *)malloc(LEN); printf("Please enter the num : "); scanf("%d", &p1->num); printf("Please enter the score : "); scanf("%f", &p1->score); head = NULL; n=0; while(0 != p1->num) { n++; if(1 == n) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct student *)malloc(LEN); printf("\nPlease enter the num: "); scanf("%d", &p1->num); printf("\nPlease enter the score: "); scanf("%f", &p1->score); } p2->next = NULL; return head;}void print(struct student *head) { struct student *p; printf("\nThere are %d recoreds!\n",n); p=head; if( head ) { do { printf(" %d %f\n",p->num,p->score); p = p->next; } while( p ); }}

练习题

#include struct STU {char name[20];int num;int score;};void main(){ struct STU s[5] = {{"Wa",2011,703},{"Zhangsan",2012,580},{"Lisi",2013,680},{"Zhangsan",2012,550},{"Zhangsan",2012,537}}, *p[5],*t; int i,j; for(i=0;i<5;i++)p[i] = &s[i]; for(i=0;i<4;i++) for(j=i+1;j<5;j++) if(p[i]->score>p[j]->score) {t=p[i];p[i]=p[j];p[j]=t;} printf("%d%5d\n",s[0].score,p[0]->score);}

703 537

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:五大内存分区
下一篇:BetePHP是一个易于学习使用的高性能PHP框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~