c语言链表:编写input()output()insert()delete()来输入学生成绩

#include<stdio.h>
#include<stdlib.h>
struct Student
{
 int studid;  /*学号*/
 char name[10];  /*姓名*/
 int score1;   
 int score2;
 int score3;
 int total;       /*总成绩*/
 struct Student *next; /*指向下一个结点的指针*/
};
void inputlist(struct Student *phead)
{
   struct Student *p; 
    while(1)
 {
    p = (struct Student*)malloc(sizeof(struct Student));
    printf("请输入学生学号:");
    scanf("%d", &(p->studid));//相当于-1为终止输入符号
    if (p->studid==-1 )
    { 
        free(p);
        break;
    }
    printf("请输入学生姓名:");
    scanf("%s", &(p->name));
    printf("请输入第一科成绩:");
    scanf("%d", &(p->score1));
    printf("请输入第二科成绩:");
    scanf("%d", &(p->score2)) ;
    printf("请输入第三科成绩:");
    scanf("%d", &(p->score3)) ;
    p->total = p->score1+ p->score2+p->score3;
    p->next = phead->next;//相当于p的尾结点指向了null
    phead->next = p;//相当于phead的尾节点从指向null指向了p的头节点
    printf("\n");
    printf("若想终止输入数据,请输入学号为-1");
    printf("\n");
  }
}
void print(struct Student *phead)
{   
    struct Student *p; 
    p = (struct Student*)malloc(sizeof(struct Student));
    p= phead->next;
   printf("学号   姓名    第一科  第二科  第三科  总分 \n");
   while(p!=NULL)    
    {   
       
        printf("%d\t", p ->studid);
        printf("%s\t", p ->name);
        printf("%d\t", p ->score1);
        printf("%d\t", p ->score2);
        printf("%d\t", p ->score3);
        printf("%d\t", p ->total);
        printf("\n");
        p = p ->next;
    }
}
void max(struct Student *phead)
{   
    struct Student *p,*q;
    p=phead->next; //可以不用申请空间了 
    q=p->next;//使得指向下一个链表
    while( (p->next)!=NULL)
    { 
      if ((p->total)<=(q->total))
        {
            p=q;
        }
      q=q->next;
    } 
    printf("\n");
    printf("平均分最高分的同学数据为:\n");
    printf("学号   姓名    第一科  第二科  第三科  总分 \n");
    printf("%d\t", p ->studid);
    printf("%s\t", p ->name);
    printf("%d\t", p ->score1);
    printf("%d\t", p ->score2);
    printf("%d\t", p ->score3);
    printf("%d\t", p ->total);
}
void insert(struct Student *phead)
{
 struct Student *p;
 p=(struct Student*)malloc(sizeof(struct Student));
 printf("\n输入需要插入的学生数据\n"); 
 printf("学        号:");
 scanf("%d", &(p->studid));
 printf("请输入学生姓名:");
 scanf("%s", &(p->name));
 printf("请输入第一科成绩:");
 scanf("%d", &(p->score1));
 printf("请输入第二科成绩:");
 scanf("%d", &(p->score2));
 printf("请输入第三科成绩:");
 scanf("%d", &(p->score3));
 p->next = phead->next;
 phead->next = p;
}
void delete(struct Student *phead)
{  
   int dlt=0;
	struct Student *p,*q;
	p=phead;
	q=phead->next;
	printf("\n输入需要删除的学生数据的学号:"); 
	scanf("%d",&dlt); 
	while(q!=NULL)    
	{ 
		if(q->studid==dlt)
		{
			p->next=q->next;
			free(p);
			break;	
		}
		p=q;
		q = p->next;
    }
  print(phead);
}
void main ()
{  
  struct Student *phead;
  phead =( struct Student *)malloc(sizeof(struct Student));//申请了一个结构体指针,该指针类型有尾标(phead->next),利用这个尾标来建立链表
  phead->next=NULL;
  inputlist(phead);
  print(phead); 
  max(phead);
  insert (phead);
  delete(phead);
   
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值