基于B+树的学生信息管理系统

这篇博客介绍了B+树的数据结构及其在数据库底层管理中的应用。通过提供的代码展示了如何使用B+树进行数据的读取、插入和导出。`get_info`函数用于从CSV文件中读取学生信息并构建B+树,而`write_data`函数则将数据写入到文本文件。同时,还提供了一个`IsExist`函数用于查询B+树中节点是否存在。完整工程可在链接中下载。

知识准备

b+树线程代码很少,主要用于数据库底层管理,如下两份资请自行阅读。

https://www.bilibili.com/video/BV1n7411A7x3?from=search&seid=7003654934148976244
https://blog.youkuaiyun.com/xiaohusaier/article/details/77101640

需求

在这里插入图片描述

重点代码

B+树相关代码出自另一篇博客,引用如上。
如下是本人写的,readdata执行数据的录入和导出。

//readdata.h
#ifndef READDATA_H_INCLUDED
#define READDATA_H_INCLUDED
#include <stdio.h>
#include "bplusTree.h"
struct student{
    int id,grade;
    char gender[10];
    char name[30];
};
extern struct student st[5000000];
BPlusTree get_info(BPlusTree T);
void write_data(BPlusTree T);
#endif // READDATA_H_INCLUDED
//readdata.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "readdata.h"
#include "bplusTree.h"

#define FILENAME "info.csv"
#define OUTFILENAME "out.txt"
struct student st[5000000];
BPlusTree  get_info(BPlusTree T){
    FILE *fp = NULL;
	char *line,*record;
	char buffer[1024];
	if ((fp = fopen(FILENAME, "at+")) != NULL)
	{
		int j,id;
		fgets(buffer, sizeof(buffer), fp);
		while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
		{
			record = strtok(line, ",");
			j=0;
			while (record != NULL)//读取每一行的数据
			{
			    switch(j)
			    {
                case 0:
                    id=atoi(record);
                    T = Insert(T, id);
                    st[id].id=id;
                    break;

                case 1:
                    strcpy(st[id].gender,record);
                    break;

                case 2:
                    strcpy(st[id].name,record);
                    break;

                case 3:
                    st[id].grade=atoi(record);
                    break;
			    }
				//printf("%s ", record);//将读取到的每一个数据打印出来
				record = strtok(NULL, ",");
				j++;
			}
			printf("\n");

		}
		fclose(fp);
		fp = NULL;
	}
    return T;
}
void write_data(BPlusTree T){
    FILE* fp1 = fopen(OUTFILENAME , "w");
    Position Tmp=FindFirst(T);
    int id,i;
    /* 第一片树叶 */
    while (Tmp != NULL){
        i = 0;
        while (i < Tmp->KeyNum){
            id=Tmp->Key[i++];
            fprintf(fp1, "%d,%s,%s,%d\n",\
                     st[id].id,st[id].name,st[id].gender,st[id].grade);

        }
        Tmp = Tmp->Next;
    }
    fclose(fp1);//关闭文件
    printf("Data saved.\n");
}

查询该节点是否存在,模拟磁盘的思路,磁盘中的文件如果删除,知识索引删除,文件实体暂时不会更新。

extern Position FindFirst(BPlusTree T){
    Position Tmp;
    if (T == NULL)
        return T;
    Tmp = T;
    while (Tmp->Children[0] != NULL)
        Tmp = Tmp->Children[0];
    return Tmp;
}
extern int IsExist(BPlusTree T,KeyType Key){
    int  j = 0,i;
    while(j<T->KeyNum){
        //printf("%d\r\n",T->Key[j]);
        if(j==0 && Key<T->Key[j]){
            return -1;
        }else if(Key == T->Key[j]){
            return 1;
        }else if((j<T->KeyNum-1 && Key>=T->Key[j] && Key<T->Key[j+1])\
                 ||(j==T->KeyNum-1 && Key>=T->Key[j])){
            if(T->Children[j]==NULL) {
                   // printf("%d\r\n",T->Key[j]);
                for(i=0;i<T->KeyNum;i++){
                    if(T->Key[i]==Key) return 1;
                }
                return -1;
            }
            else return IsExist(T->Children[j],Key);
        }
        j++;
    }
    return 0;
}

完整工程

https://download.youkuaiyun.com/download/renzemingcsdn/19688648

效果

数据导入
在这里插入图片描述

查找
在这里插入图片描述
删除后 查无此人
在这里插入图片描述
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清欢_小铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值