一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息

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

typedef struct stu
{
    char name[20];
    int  no;
    char sex;
    int  age;
    stu *next;
};

stu *CreatList()
{
    int n;
    printf("Please input the number of students:");
    scanf_s("%d", &n);

    stu *head, *pre,*cur;
    head = new stu();
    //head = (stu*)malloc(sizeof(stu));
    head->next = NULL;
    pre = head;
    for(int i = 0; i < n; i++)
    {
        cur = new stu();
        //cur = (stu*)malloc(sizeof(stu));
        printf("Please input the information of students: name no age sex(b|g) :\n");
        scanf_s("%s",cur->name, 20);
        scanf_s("%d %d %c", &cur->no, &cur->age, &cur->sex);
        cur->next = NULL;
        pre->next = cur;
        pre = pre->next;
    }
    return head;
}

void DeleteByAge(stu* &head)
{
    int age;
    printf("Please input the age of students that you want to delete:");
    scanf_s("%d", &age);

    stu *pre, *cur;
    pre = head;
    cur = pre->next;
    while(cur != NULL)
    {
        if(cur->age == age)
        {
            pre->next = cur->next;
            delete cur;
            //free(cur);
            //cur->next = NULL;
            cur = pre->next;
        }
        else
        {
            pre = cur;
            cur = cur->next;
        }
    }
}

void Display(stu* &head)
{
    stu *s;
    s = head->next;
    while(s != NULL)
    {
        printf("%s %d %d %c\n", s->name, s->no, s->age, s->sex);
        s = s->next;
    }
}

int main()
{
    stu *head;
    head = CreatList();
    Display(head);
    DeleteByAge(head);
    Display(head);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值