6-2 学生成绩链表处理

6-2 学生成绩链表处理

分数 20

全屏浏览题目

切换布局

作者 C课程组

单位 浙江大学

本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

函数接口定义:

 

struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );

函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:

 

struct stud_node { int num; /*学号*/ char name[20]; /*姓名*/ int score; /*成绩*/ struct stud_node *next; /*指向下个结点的指针*/ };

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。

裁判测试程序样例:

 

#include <stdio.h> #include <stdlib.h> struct stud_node { int num; char name[20]; int score; struct stud_node *next; }; struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score ); int main() { int min_score; struct stud_node *p, *head = NULL; head = createlist(); scanf("%d", &min_score); head = deletelist(head, min_score); for ( p = head; p != NULL; p = p->next ) printf("%d %s %d\n", p->num, p->name, p->score); return 0; } /* 你的代码将被嵌在这里 */

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80

输出样例:

2 wang 80
4 zhao 85

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

/* 你的代码将被嵌在这里 */
/* 你的代码将被嵌在这里 */
struct stud_node *createlist(){
    struct stud_node *phead = (struct stud_node*)malloc(sizeof(struct stud_node));
    struct stud_node *ptail;
    phead = ptail = NULL;

    while(1){
        struct stud_node *pnew = (struct stud_node*)malloc(sizeof(struct stud_node));
        scanf("%d",&pnew->num);
        pnew->next = NULL;
        if(pnew->num==0){
            break;
        }
        scanf("%s %d",&pnew->name,&pnew->score);
        if(phead == NULL){
            phead = pnew;
        }else{
            ptail->next = pnew;
        }
        ptail = pnew;
    }
    return phead;
};
struct stud_node *deletelist( struct stud_node *head, int min_score ){
    struct stud_node *pcurrent,*pbefore;
    //表为空
    if(head==NULL){
        return NULL;
    }
    pcurrent = head;
    //如果删除的结点为头节点
    while(pcurrent==head&&head!=NULL){
        if(pcurrent->score<min_score){
            head = head->next;
            free(pcurrent);
            pcurrent = head;//删除头结点之后,下一个点也为头节点
        }else{//头结点不满足删除条件
            pbefore = head;
            pcurrent = head->next;
        }
    }
    //删除的结点不为头节点
    while(pcurrent!=NULL&&pbefore!=NULL){
        if(pcurrent->score<min_score){//需要删除的情况
            if(pcurrent->next!=NULL){//当前结点不为尾结点
                pbefore->next = pcurrent->next;//满足条件则跳过当前结点
            }else{//当前结点为尾结点
                pbefore->next = NULL;
            }
            free(pcurrent);//释放结点
        }

        pbefore = pbefore->next;//到下一个结点
        if(pbefore!=NULL){//当前未循环到末尾
           pcurrent = pbefore->next;
        }
    }
    return head;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值