C语言链表学习--学生信息管理系统

最近学习了下C语言的链表,为了更加熟悉链表,就随意写了个学生信息管理系统(写的很垃圾,大佬勿喷)

本人最近刚好有点时间,就去看了看c语言的链表,看到网上各种c语言的学生信息管理代码,确实都是现成的东西,我也没必要去自己再写,但是本萌新是带着学习的态度好吧(内心真实想法:我就是闲的蛋疼好吧 ),决定自己尝试去写一段这个代码,毕竟只有自己会写才是真理,你看的代码再多,你看的时候觉得你会了,其实不然,说到这里很多人就不信了,这么个简单的代码wo还不会了,那好,你把代码关掉,当场给我写。(暴躁老哥,在线GAY人)。好啦,其实我想说的是,别人的代码写的再好,那也是别人的,始终不是你的,所以,everbody,跟着我一起快乐的撸代码吧。最后由于本人能力有限,代码之处可能存在不少BUG,如有发现,请当场给我两个耳刮子,让我长长记性,学习学习(说着玩的,不要真的抽我好吧)。
备注:因为只是想深入了解下链表,此处所做的信息管理系统,没有很好的完善界面和选择功能,只是将各个功能都体现出来,后续如有时间可以尝试弄下。(我就是没时间,就是不弄,来打我啊!
快来干我啊,顺着网线过来戳死我。

// 该代码实现学生数据库管理系统
// 所实现的功能为:1.创建学生信息库 2.增加和删除学生信息 3.显示所有学生信息 4.保存所有学生信息
// 代码完成时间:2019/8/16
// 代码版本:V1.0
// 完成人:wangming
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <malloc.h>
struct stu
    {
        char name[20];
        int score;
        char qq[10];
        char from[15];
        struct stu *next;
    };
struct stu *head;
// 函数声明
void creat();                                   // 创建学生信息库函数
void sprint(struct stu *u);                     // 打印显示所有学生信息函数
void add(struct stu *head);                     // 增加学生信息函数
void save(struct stu *head);                    // 保存学生信息函数
void read();                                    // 从文件中读取学生信息函数
void delete(struct stu *head);                  // 删除学生信息函数
// 主函数,里面的功能全部用各个函数封装起来
int main()
{   
    struct stu *p;
    creat();
    sprint(head);
    add(head);
    sprint(head);
    save(head);
    read();
    delete(head);
    sprint(head);
}

void add(struct stu *head)
{
    struct stu *p,*p1,*p2,*p3;
    p = head;
    char name[20];
    printf("please input the name of you want to add:");
    scanf("%s",&name);
    p1 = (struct stu *)malloc(sizeof(struct stu));
    while(p!=NULL)
    {
        if(strcmp(p->name,name)==0)
        {   
            p2 = p;
            break;
        }
        p = p->next;
    }
    if(p==NULL)
    {
        printf("not find this student's name\n");
    }
    else
    {
        printf("find this student's name\n");
        printf("-----------------------------------");
        printf("please input student's info\n");
        printf("please input name:");
        scanf("%s",&p1->name);
        printf("please input score:");
        scanf("%d",&p1->score);
        printf("please input qq:");
        scanf("%s",&p1->qq);
        printf("please input from:");
        scanf("%s",&p1->from);
        printf("--------------------------------------\n");
        p3 = p2->next;
        p2->next = p1;
        p1->next = p3;
    }
}

void creat()
{
    struct stu *p1,*p2;
    head = NULL;
    p2 = p1 = (struct stu *)malloc(sizeof(struct stu));
    // printf("please input info:");
    head = p1;
    // scanf("%s",&p1->name);
    int flag;
    flag = 1;
    while(flag)
    {   
        printf("please input name info:");
        //head = p1;
        scanf("%s",&p1->name);
        if(strcmp("ok",p1->name)==0)
        {
            flag = 0;
            p2->next = NULL;
            printf("input success\n");
        }
        else
        {
            printf("please input score:");
            scanf("%d",&p1->score);
            printf("please input qq:");
            scanf("%s",&p1->qq);
            printf("please input from:");
            scanf("%s",&p1->from);
            printf("--------------------------------------\n");
            p2 = p1;
            p1 = (struct stu *)malloc(sizeof(struct stu));
            if(p1 == NULL)
            {
                printf("failed get memery\n");
            }
            p2->next = p1;
        }
    }
}

void sprint(struct stu *u)
{   
    struct stu *p;
    p = head;
    while(p!=NULL)
    {   
        printf("--------------------------------------\n");
        printf("name:%s\n",p->name);
        printf("name:%d\n",p->score);
        printf("name:%s\n",p->qq);
        printf("name:%s\n",p->from);
        printf("--------------------------------------\n");
        p = p->next;
    }
}

void save(struct stu *p)
{
    FILE *fp;
    fp = fopen("D:\\student.txt","w");
    while(p!=NULL)
    {   
        fprintf(fp,"--------------------------------------\n");
        fprintf(fp,"%s\n",p->name);
        fprintf(fp,"name:%d\n",p->score);
        fprintf(fp,"name:%s\n",p->qq);
        fprintf(fp,"name:%s\n",p->from);
        fprintf(fp,"--------------------------------------\n");
        p = p->next;
    }
    return ;
}

void read()
{
    FILE *fp;
    char ch;
    fp = fopen("D:\\student.txt","r");
    while(ch = fgetc(fp)!=EOF)
    {
        putchar(ch);
    }
    return ;
}

void delete(struct stu *u)
{
    struct stu *p,*p1;
    char name[20];
    p = u;
    printf("(delete)please input name:");
    scanf("%s",&name);
    if(strcmp(u->name,name)==0)      // 如果链表的头部就是需要删除的信息,那就将头部信息的地址更换为head->next
    {
        head = u->next;
        return ;
    }
    while(p!=NULL)                      // 如果不是链表的头部信息,那就通过遍历链表查询,删除掉需要删除的信息
    {
        if(strcmp(p->name,name)==0)
        {
            printf("find this name\n");
            p = p->next;
            break;
        }
        p1 = p;
        p = p->next;
    }
    if(p==NULL)
        {
            printf("not find\n");
        }
    p1->next = p;
    return ;
}

如有错误,请各位大哥大姐不吝自己的意见,批判提出,大家共同进步。哈哈哈哈哈哈。

猜你喜欢

转载自blog.csdn.net/qq_36662353/article/details/99679556