创建学生成绩链表,并删除低于一定分数的节点

先建立学生成绩链表,然后删除那些分数低于一定值的节点

(多了个删除节点

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
    int num;
    char name[10];
    int score;
    struct student *next;
}Student;

// typedef struct _list
// {
//     Student head;
// }List;


Student *Creat_Stu_Doc();   //创建链表
Student *DeleteDoc(Student*head,int min_score); //分数低于..删除
void Ptrint_Stu_Doc(Student *head);    //打印链表

int main()
{
    // List list;
    // list.head.next=NULL;
    // int number;
    // scanf("%d", &number);
    // if(number != 0){
    //     struct stud_node *Creat_Stu_Doc();
    // }

    Student *head;
    int minscore;
    head = Creat_Stu_Doc();
    scanf("%d", &minscore);
    Student *new = DeleteDoc(head, minscore);
    Ptrint_Stu_Doc(new);
    return 0;

}

Student *Creat_Stu_Doc()
{
    //List list;
    int number;
    Student *head=NULL, *tail=NULL;
    scanf("%d", &number);
    do{
        Student *p = (Student *)malloc(sizeof(Student));
        scanf("%s%d", &p->name, &p->score);
        p->num=number;
        p->next=NULL;

        if(head==NULL){
            head=p;
        }
        else{
            tail->next=p;
        }
        tail=p;
        
        scanf("%d", &number);
    }while(number!=0);

    return head;
}

Student *DeleteDoc(Student *head,int min_score)
{
    Student *p, *q;
    while(head != NULL&& head->score < min_score){
        p=head;
        head=head->next;
        free(p);
    }

    if(head==NULL)
        return NULL;
    
    else{
        for(q=NULL, p=head; p;q=p, p=p->next){
            if(p->score < min_score){
                q->next=p->next;
                free(p);
            }
        }
    }

    return head;
}

void Ptrint_Stu_Doc(Student *head)
{
    while(head!=NULL){
        printf("%d %s %d\n", head->num, head->name, head->score);
        head = head->next;
    }
    return;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值