学生信息管理系统( 单链表知识)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stud   /*单链表类型定义, 记录学生基本信息*/
{
    char id[13];
    char name[21];
    int score1;
    int score2;
    int score3;
    int sum;
    struct stud *next;
}LNode, *Linklist;

void createstulist(Linklist head);  /*创建单链表*/
void printstulist(Linklist head);   /*遍历单链表*/
void stulistinsert(Linklist head);  /*插入元素*/
void stulistdelete(Linklist head)  ;/*删除元素*/
void staticscorelist(Linklist head);  /*统计元素*/
int main()
{
    Linklist head;
    int select;

    head=(Linklist)malloc(sizeof(LNode));
    head->next=NULL;
    while(1)
    {
        printf("=============================\n");
        printf("*欢迎进入学生信息管理系统*\n");
        printf("1: 创建链表\n");
        printf("2: 插入元素\n");
        printf("3: 删除元素\n");
        printf("4: 显示元素\n");
        printf("5: 统计不及格学生\n");
        printf("0: 退出系统\n");
        printf("=============================\n");
        printf("请输入菜单项:\n");
        scanf("%d", &select);

        switch(select)
        {
        case 1:
            printf("请输入若干整数, 输入#表示输入结束\n");
            createstulist(head);
            printf("\n新创建的链表中学生信息为:\n");
            printstulist(head);
            printf("\n");
            break;
        case 2:
            printf("当前链表中学生信息为:\n");
            printstulist(head);
            printf("\n请输入待添加学生的信息\n");
            stulistinsert(head);
            printf("\n链表修改后的学生信息为:\n");
            printstulist(head);
            break;
        case 3:
            printf("当前链表中学生信息为:\n");
            printstulist(head);
            printf("\n请输入待删除学生的信息\n");
            stulistdelete(head);
            printf("\n链表修改后的学生信息为:\n");
            printstulist(head);
            break;
        case 4:
            printf("当前链表中的学生信息为:\n");
            printstulist(head);
            break;
        case 5:
            printf("当前链表中学生信息为:\n");
            printstulist(head);
            printf("有不及格成绩的学生信息统计结果为:\n");
            staticscorelist(head);
            break;
        case 0:
            exit(0);
        }
    }
    return 0;
}

void createstulist(Linklist head)  /*创建单链表*/
{
    Linklist p, rear=head;
    LNode newstu;
    while(scanf("%s", newstu.id), strcmp(newstu.id, "#"))   /*输入学生信息, 输入#表示结束*/
    {
        scanf("%s %d %d %d", newstu.name, &newstu.score1, &newstu.score2, &newstu.score3);
        p=(Linklist)malloc(sizeof(LNode));
        strcpy(p->id, newstu.id);
        strcpy(p->name, newstu.name);
        p->score1=newstu.score1;
        p->score2=newstu.score2;
        p->score3=newstu.score3;
        p->sum=newstu.score1+newstu.score2+newstu.score3;
        rear->next=p;
        rear=p;
    }
    rear->next=NULL;
}

void printstulist(Linklist head)   /*遍历单链表*/
{
    Linklist p=head->next;
    while(p)
    {
        printf("%s %s %d %d %d %d\n", p->id, p->name, p->score1, p->score2, p->score3, p->sum);
        p=p->next;
    }
}

void stulistinsert(Linklist head)  /*插入元素*/
{
    Linklist p=head->next, q, pre=head;
    int i=1, j=0, k=0;
    LNode newstu;    /*定义一个结构体来记录插入的学生信息*/
    scanf("%s %s %d %d %d", newstu.id, newstu.name, &newstu.score1, &newstu.score2, &newstu.score3);
    while(p)
    {
        if(strcmp(p->id, newstu.id)==0)
        {
            printf("学号已有, 无法添加!\n");
            k=1;
        }
        else if(strcmp(p->id, newstu.id)<0)
        {
            i++;
        }
        p=p->next;
    }
    if(k!=1)
    {
        while(pre&&j<i-1)
        {
            pre=pre->next;
            j++;
        }
        q=(Linklist)malloc(sizeof(LNode));   /*创建新结点, 接收学生信息并插入到链表中*/
        strcpy(q->id, newstu.id);
        strcpy(q->name, newstu.name);
        q->score1=newstu.score1;
        q->score2=newstu.score2;
        q->score3=newstu.score3;
        q->sum=newstu.score1+newstu.score2+newstu.score3;
        q->next=pre->next;
        pre->next=q;
    }
}
void stulistdelete(Linklist head)   /*删除元素*/
{
    Linklist p=head->next, q, pre=head, p1=head->next;
    LNode newstu;
    int i=0, j=0, k=0;
    scanf("%s", newstu.id);
    while(p1)
    {
        if(strcmp(p1->id, newstu.id)==0)
        {
            i=1;
        }
        p1=p1->next;
    }
    if(i!=1)
    {
        printf("查无此号, 无法删除!\n");
    }
    else
    {
        while(p)
        {
            if(strcmp(p->id, newstu.id)<=0)
            {
                j++;
            }
            p=p->next;
        }
        while(pre&&k<j-1)
        {
            pre=pre->next;
            ++k;
        }
        q=pre->next;
        pre->next=q->next;
        free(q);
    }
}

void staticscorelist(Linklist head)  

/*统计元素*/
{
    Linklist p=head->next;
    while(p)
    {
        if(p->score1<60||p->score2<60||p->score3<60)
        {
            printf("%s %s %d %d %d %d\n", p->id, p->name, p->score1, p->score2, p->score3, p->sum);
        }
        p=p->next;
    }
}

转载于:https://my.oschina.net/waitingshanshan/blog/663484

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值