数据结构-文件处理

文件载入内存,生成链表实现查询,删除,修改
student1.txt
182544,陆一在,18911555661
193752,孙训兰,18911555662
197132,程昕,18911555663
205844,王亚威,18911555664

bdfunction.h

#pragma once
#include <stdio.h>
#include<stdlib.h>
#include"memfunction.h"//内存管理函数的头文件

//定义链表结构(指针链表void*)

#define FILESIZE 309068//文件大小
#define LMAX 256//行最大数


struct strudent//学生信息
{
	char  name[19];
	int id;
	long long int phone;
};

typedef struct LinkNode
{
	void * data;
	LinkNode * pre;//前驱
	LinkNode * next;//后续
}*linknode;

void addlink(LinkNode **head,void * data);//添加节点
void showlist(LinkNode const *head, void(showcon)(void *));//显示链表
void * dellink(LinkNode **head, int(finddata)(void *));//删除节点

void * findlink(LinkNode *head, int(finddata)(void *));//查找节点
void * changelink(LinkNode *head, int(finddata)(void *), void (changedata)(void *));//更改节点

int initfiletomem(char *filepath);//文件导入内存

bdfunction.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<stdarg.h>
#include<string.h>
#include "bdfunction.h"
#define   malloc mymalloc
#define free myfree


void addlink(LinkNode **head, void * data)//添加节点
{
	if ((*head) == NULL) //头节点为空
	{
		(*head) = (LinkNode*)malloc(sizeof(LinkNode));
		(*head)->data = data;
		(*head)->pre = NULL;
		(*head)->next = NULL;
	}
	else
	{
		LinkNode* temp = (*head);
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		temp->next= (LinkNode*)malloc(sizeof(LinkNode));
		temp->next->data = data;
		temp->next->next = NULL;
		temp->next->pre = temp;
	}
	
}

void showlist(LinkNode const *head,void(showcon)(void *))
{
	if (head == NULL)
	{
		printf("没有内容\n");
		return;
	}
	else
	{
		while (head != NULL)
		{
			//根据链表中的指针打印所对应数据结构的内容,传入一个函数指针
			showcon(head->data);
			head = head->next;
		}

	}
}

void * dellink(LinkNode **head, int(finddata)(void *))//删除节点
{
	int flag = 0;
	if ((*head) == NULL)
	{
		printf("链表没有内容\n");
		return NULL;
	}
	else
	{
		LinkNode *temp = (*head);
		while (temp != NULL)
		{
			if (finddata(temp->data))//找到元素
			{
				if (temp->next == NULL&&temp->pre == NULL)//只有一个元素
				{
					temp->data = NULL;
					free(temp);
					temp = NULL;
					(*head) = NULL;
				

				}else if (temp->next == NULL)//元素在最后一个
				{
					temp->pre->next = NULL;
					temp->data = NULL;
					free(temp);
					temp = NULL;

				}else if(temp->pre==NULL)	//元素在第一个
				{
					(*head) = temp->next;
					(*head)->pre = NULL;
					temp->data = NULL;
					free(temp);
					temp = NULL;
				
				}else //元素在在中间
				{
					temp->pre->next = temp->next;
					temp->next->pre = temp->pre;
					temp->data = NULL;
					free(temp);
					temp = NULL;
				}
				flag = 1;
				break;
			}
			temp = temp->next;	
		}
	}

	if (flag == 1)
		return finddata;
	else
		return NULL;
}

//dellink_a("%d&%s",id,name)//查找删除id=?跟name=?的链表节点

void * findlink(LinkNode *head, int(finddata)(void *))//查找节点
{
	if (head == NULL)
	{
		printf("链表没有内容\n");
		return NULL;
	}
	else
	{
		LinkNode *temp = head;
		while (temp != NULL)
		{
			if (finddata(temp->data))//找到
			{
				printf("\n找到的数据地址%p\n", temp->data);
				return temp->data;
				break;
			}
			temp = temp->next;
		}
		printf("没有找到数据\n");
		return NULL;
	}
}

void * changelink(LinkNode *head, int(finddata)(void *),void (changedata)(void *))//更改节点
{
	if (head == NULL)
	{
		printf("链表没有内容\n");
		return NULL;
	}
	else
	{
		LinkNode *temp = head;
		while (temp != NULL)
		{
			if (finddata(temp->data))//找到
			{
				printf("\n找到的数据地址%p\n", temp->data);
				changedata(temp->data);
				return temp->data;
				break;
			}
			temp = temp->next;
		}
		printf("没有找到数据\n");
		return NULL;
	}
}

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"bdfunction.h"
#include<process.h>

char filepath[256] = "student1.txt";//定义文件路径
char findstr[19],changestr[19];

void showcon(void * ptr)//根据指针地址显示内容
{
	if (ptr == NULL)
		return;
	printf("\n%s,%d,%lld", ((strudent *)ptr)->name, ((strudent *)ptr)->id, ((strudent *)ptr)->phone);
}


int fdata(void * ptr)//根据指针查找内容返回找到或没找到
{
	if (!strcmp(((strudent*)ptr)->name, findstr))
		return 1;
	else
		return 0;
}

void cdata(void * cptr)//根据链表地址改变内容
{
	strcpy(((strudent *)cptr)->name,changestr);
}

LinkNode *head = NULL;

int initfiletomem(char *filepath)//文件导入内存,创建链表
{
	//setlocale(LC_ALL, NULL);
	FILE *pfr = fopen(filepath, "rb");
	char lstr[LMAX] = { 0 };
	if (pfr == NULL)
	{
		printf("文件打开失败");
	}
	else
	{
		while (!feof(pfr))
		{
			fgets(lstr, LMAX, pfr);

			strudent * st=NULL;
			st = (strudent *)malloc(sizeof(strudent));
			int i = 0;
			char templstr[LMAX] = { 0 };
			strcpy(templstr,lstr);
			int lenght = 0;
			while (templstr[i] != '\0')//取id
			{	
				if (templstr[i] == ',')
				{
					templstr[i] = '\0';
					lenght = strlen(templstr);
					st->id= atoi(templstr);
					break;
				}
				i++;
			}
			i = 0;
			strcpy(templstr, lstr + lenght + 1);
			while (templstr[i] != '\0')//取姓名
			{
				if (templstr[i] == ',')
				{
					templstr[i] = '\0';
					 lenght = lenght+strlen(templstr);
					strcpy(st->name, templstr);
				}
				i++;
			}
			
			strcpy(templstr, lstr + lenght + 2);
			i = 0;
			while (templstr[i] != '\0')//取电话
			{
				if (templstr[i] == '\r')
				{
					templstr[i] = '\0';
					st->phone = atoll(templstr);
				}
				i++;
			}

			addlink(&head, st);//加入链表
		}
	}
	printf("文件载入内存成功");
	fclose(pfr);
	return 1;
}


void main()
{
	initfiletomem(filepath);//文件载入内存

	printf("内存使用了字节:%d,多少块:%d\n",mcount().size,mcount().sum);//内存使用情况

	//查找
	while (1)
	{
		printf("\n查找到的数据为:");
		scanf("%s", findstr);
		showcon(findlink(head, fdata));//打印找到的数据
		if (findstr[0] == '#')
			break;
		printf("\n");
	}
	//删除
	while (1)
	{
		printf("\n您要删除的数据为:");
		scanf("%s", findstr);
		showcon(findlink(head, fdata));//打印找到的数据
		dellink(&head, fdata);
		if (findstr[0] == '#')
			break;
		printf("\n");

	}

	//更改节点
	while (1)
	{
		printf("\n您要改变的数据为:");
		scanf("%s", findstr);
		printf("\n改变的数据为");
		scanf("%s", changestr);
		changelink(head, fdata, cdata);
		showcon(findlink(head, fdata));//打印找到的数据
		if (findstr[0] == '#')
			break;
		printf("\n");

	}
	system("pause");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值