线性表链表

//输入n个学生信息(姓名,成绩),再按成绩降序输出这些信息
#include<stdio.h>
#include<stdlib.h>
//------------------------
struct student
{
 char name[10];
 float score;
 struct student *next;
};
//------------------------
struct student *insert(struct student *head)
{
 struct student *p,*pnew,*pold;
 pnew=(struct student *)malloc(sizeof(struct student));
 scanf("%s%f",pnew->name,&pnew->score);//建立新节点
 p=head;
 if(pnew->score>head->score)
 {
  pnew->next=p;
  head=pnew;
 }
 else
 {
  while(p!=NULL&&pnew->score<p->score)
  {
   pold=p;
   p=p->next;
  }
  pnew->next=p;
  pold->next=pnew;
 }
 return head;
}
//------------------------
void print(struct student *head)
{
 struct student *p=head;
 while(p!=NULL)
 {
  printf("%s%.1f/n",p->name,p->score);
  p=p->next;
 }
}
//------------------------
void main()
{
 struct student *head;
 int i,n;
 scanf("%d",&n);
 head=(struct student *)malloc(sizeof(struct student));
 scanf("%s%f",head->name,&head->score);
 head->next=NULL;
 for(i=1;i<n;i++)
 {
  head=insert(head);
 }
 print(head);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值