C语言链表实现班级管理系统

     上次用结构体数组做了一个图书管理系统,这次决定用链表实现一个简单的班级管理系统,还是先用create()函数第一次输入内容,之后使用可以直接从文件中读取。
#include<stdio.h>
#include<stdlib.h>
#define M 20


//定义学生的结构体
 typedef struct noab
 {
    int id;
    char name[M];
    char classroom[M];
    float score;
    struct node *next;
 }student;
student *init()
{
    student *p,*q,*head;
    int i,a;
    int n=0;
    printf("输入学生信息的个数\n");
    scanf("%d",&a);

    head=NULL;
    for(i=0;i<a;i++)
    {
        p=(student *)malloc(sizeof(student));
        printf("输入第%d个学生的信息\n",i+1);
        printf("学号:");
        scanf("%d",&p->id);
        printf("姓名:");
        getchar();
        gets(p->name);
        printf("班级:");
        gets(p->classroom);
        printf("成绩:");
        scanf("%f",&p->score);
        n=n+1;
        if(n==1)
        {
            head=p;
            q=p;
            q->next=NULL;

        }else
        {
            q->next=p;
            q=p;
            p->next=NULL;
        }
    }
    return head;
}
void display (student *head)
{
    student *p=head;
    while(p)
    {
        printf("学生的学号为%d,姓名为%s,班级为%s,分数为%f\n",p->id,p->name,p->classroom,p->score);
        p=p->next;
    }
}

student *addStudent(student *head)
{
    student *p,*q;
    q=head;
    while(q->next) q=q->next;
     p=(student *)malloc(sizeof(student));
        printf("学号:");
        scanf("%d",&p->id);
        printf("姓名:");
        getchar();
        gets(p->name);
        printf("班级:");
        gets(p->classroom);
        printf("成绩:");
        scanf("%f",&p->score);
        p->next=NULL;
        q->next=p;
        return head;
}

student *deleteStudent(student *head)
{
    int a;
    student *pre=NULL,*p;
    printf("请输入要删除的id");
    scanf("%d",&a);
    if(!head) {printf("系统中已经没有数据可以删除\n");return head;}
    p=head;
    while(p&&p->id!=a)
    {
        pre=p;
        p=p->next;
    }
    if(p)
    {
        if(!pre) head =head->next;
        else pre->next=p->next;
        free(p);
    }
    return head;

}

student *correctStudent(student *head)
{
    int a;
    student *p;
    printf("请输入要修改的学生的id");
    scanf("%d",&a);
    if(!head) {printf("系统中已经没有数据可以修改\n");return head;}
    p=head;
    while(p&&p->id!=a)
    {
        p=p->next;
    }
    if(p==NULL){printf("没有查到要修改的学生的id\n");return head;}
    else
    {
        printf("学号:");
        scanf("%d",&p->id);
        printf("姓名:");
        getchar();
        gets(p->name);
        printf("班级:");
        gets(p->classroom);
        printf("成绩:");
        scanf("%f",&p->score);
    }
    return head;
}
void writeFile(student *head)
{
    FILE *outf;
    student *p=head;
    if((outf = fopen("data.txt","wb")) == NULL) {
        printf("不能打开数据文件。\n");
        exit(1);
    }
    while(p) {
        fwrite(p,sizeof(student),1,outf);
        p = p->next;
    }
    fclose(outf);
}

student *readFile(student *head)
{
    int n=0;
    student temp;
    FILE *inf;
    student *p=NULL,*q;
    if((inf = fopen("data.txt","rb")) == NULL) {
        printf("不能打开数据文件。\n");
        exit(1);
    }
    while(fread(&temp,sizeof(student),1,inf)!=0)
    {
        p=(student *)malloc(sizeof(student));
        p->id=temp.id;
        p->score=temp.score;
        strcpy(p->name,temp.name);
        strcpy(p->classroom,temp.classroom);
        n++;
        if(n==1){head=p;q=p;}
        else
        {
            q->next=p;
            q=q->next;
        }

    }
    q->next=NULL;
    fclose(inf);
    return head;
}

void main()
{
    int a;
    student *head,*p;
    //head=init();
    head=readFile(head);

    do
    {
       printf("欢迎使用学生管理系统\n");
       printf("1:增加学生\n2:删除学生\n3:修改信息\n4:显示所有信息\n5:退出系统\n");
       printf("请输入您想进行的操作是:\n");
       scanf("%d",&a);
       switch(a)
       {
           case 1:head=addStudent(head);break;
           case 2:head=deleteStudent(head);break;
           case 3:head=correctStudent(head);break;
           case 4:display(head);break;

       }

   }while(a != 5);
   writeFile(head);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值