顺序表(文件版本)-----动态版本的进阶
创始人
2024-02-16 09:56:54
0

今天,我带来顺序表的文件版本,因为大多数代码是在动态版本顺序表的基础上,少数进行修正,所以我单独写的只是修正的部分。



目录

    • 顺序表(文件版本)的三个文档
    • 初始化函数
    • 信息存入文件的函数
    • 从文件读取信息的函数
    • test.c文档的代码
    • SeqList.h文档的代码
    • SeqList.c文档的代码



顺序表(文件版本)的三个文档

SeqList.h--------头文件的引用和函数的声明
SeqList.c--------函数的定义
test.c--------顺序表的检验



初始化函数

//初始化函数
void InitSeqList(SL* ps)
{assert(ps != NULL);ps->data = NULL;ps->size = 0;ps->capacity = 0;//读取文件信息的函数LoadSeqList(ps);
}


信息存入文件的函数

//信息存入文件的函数
void SaveSeqList(SL* ps)
{int i = 0;//打开文件FILE* pfw = fopen("test.txt","wb"); if(pfw == NULL){perror("SaveSeqList()");exit(1);}//使用文件for(i = 0; i < ps->size; i++){fwrite(&(ps->data[i]),sizeof(SeqListType),1,pfw);}if(ps->size > 0){printf("信息成功存入文件中\n");}//关闭文件fclose(pfw);pfw = NULL;
}

从文件读取信息的函数

//读取文件信息的函数
void LoadSeqList(SL* ps)
{int i = 0;SeqListType tmp = 0;//打开文件FILE* pfw = fopen("test.txt","rb");if(pfw == NULL){perror("LoadSeqList()");exit(1);}//使用文件while(fread(&tmp,sizeof(SeqListType),1,pfw)){CheckSeqList(ps);ps->data[i] = tmp;ps->size++;i++;}if(ps->size > 0){printf("从文件读取信息成功\n");}//关闭文件fclose(pfw);pfw = NULL;
}

test.c文档的代码

#include "SeqList.h"void menu()
{printf("***************************************************\n");printf("**** 1.尾插                     2.尾删         ****\n");printf("**** 3.头插                     4.头删         ****\n");printf("**** 5.中间插入数据             6.中间删除数据 ****\n");printf("**** 7.查找数据                 8.打印数据     ****\n");printf("**** 0.退出                                    ****\n");printf("***************************************************\n");
}
int main()
{int input = 0;int number = 0;int address = 0;int location = 0;SL SeqList;InitSeqList(&SeqList);do{menu();printf("请选择:>");scanf("%d",&input);switch(input){case 1:printf("请输入你要尾插的数据:>");scanf("%d",&number);SLPushBack(&SeqList,number);break;case 2:SLPopBack(&SeqList);printf("尾删成功\n");break;case 3:printf("请输入你要头插的数据:>");scanf("%d",&number);SLPushFront(&SeqList,number);break;case 4:SLPopFront(&SeqList);break;case 5:printf("请输入你要在中间插入的数据:>");scanf("%d",&number);printf("请输入你要插入的位置:>");scanf("%d",&address);SeqListInsert(&SeqList,address-1,number);break;case 6:printf("请输入你要删除的的数据的位置:>");scanf("%d",&address);SeqListErase(&SeqList,address-1);break;case 7:printf("请输入你要查找的数据:>");scanf("%d",&number);printf("请输入你要从哪个位置开始查找:>");scanf("%d",&address);location = Find(&SeqList,number,address);if(location == -1){printf("找不到\n");}else{printf("该数字的位置是%d\n",location+1);}break;case 8:Print(&SeqList);break;case 0:SaveSeqList(&SeqList);DestroySeqList(&SeqList);break;default:printf("选择错误,请重新选择\n");break;}}while(input);
}


SeqList.h文档的代码

#include
#include
#includetypedef int SeqListType;typedef struct SeqList
{SeqListType* data;int size;int capacity;
}SL;//初始化函数
void InitSeqList(SL* ps);//打印函数
void Print(SL* ps);//检查函数
void CheckSeqList(SL* ps);//查找函数
int Find(SL* ps,SeqListType x,int pos);//销毁函数
void DestroySeqList(SL* ps);//信息存入文件的函数
void SaveSeqList(SL* ps);//读取文件信息的函数
void LoadSeqList(SL* ps);//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x);//中间删除数据的函数
void SeqListErase(SL* ps,int pos);//头插函数
void SLPushFront(SL* ps,SeqListType x);//头删函数
void SLPopFront(SL* ps);//尾插函数
void SLPushBack(SL* ps,SeqListType x);//尾删函数
void SLPopBack(SL* ps);


SeqList.c文档的代码

#include"SeqList.h"//初始化函数
void InitSeqList(SL* ps)
{assert(ps != NULL);ps->data = NULL;ps->size = 0;ps->capacity = 0;//读取文件信息的函数LoadSeqList(ps);
}//打印函数
void Print(SL* ps)
{int i = 0;assert(ps != NULL);for(i = 0; i < ps->size; i++){printf("%d ",ps->data[i]);}printf("\n");
}//检查函数
void CheckSeqList(SL* ps)
{SeqListType* tmp = NULL;assert(ps != NULL);if(ps->size == ps->capacity){ps->capacity = (ps->capacity == 0? 4 : 2 * ps->capacity);tmp = (int*)realloc(ps->data,ps->capacity * sizeof(SeqListType));if(tmp == NULL){perror("CheckSeqList()");exit(1);}ps->data = tmp;}
}//销毁函数
void DestroySeqList(SL* ps)
{assert(ps != NULL);if(ps->data != NULL){free(ps->data);ps->data = NULL;ps->size = 0;ps->capacity = 0;}
}//信息存入文件的函数
void SaveSeqList(SL* ps)
{int i = 0;//打开文件FILE* pfw = fopen("test.txt","wb"); if(pfw == NULL){perror("SaveSeqList()");exit(1);}//使用文件for(i = 0; i < ps->size; i++){fwrite(&(ps->data[i]),sizeof(SeqListType),1,pfw);}if(ps->size > 0){printf("信息成功存入文件中\n");}//关闭文件fclose(pfw);pfw = NULL;
}//读取文件信息的函数
void LoadSeqList(SL* ps)
{int i = 0;SeqListType tmp = 0;//打开文件FILE* pfw = fopen("test.txt","rb");if(pfw == NULL){perror("LoadSeqList()");exit(1);}//使用文件while(fread(&tmp,sizeof(SeqListType),1,pfw)){CheckSeqList(ps);ps->data[i] = tmp;ps->size++;i++;}if(ps->size > 0){printf("从文件读取信息成功\n");}//关闭文件fclose(pfw);pfw = NULL;
}//中间插入数据的函数
void SeqListInsert(SL* ps,int pos,SeqListType x)
{int end = ps->size-1;assert(ps != NULL);assert(pos >= 0);assert(pos <= ps->size);CheckSeqList(ps);while(pos <= end){ps->data[end + 1] = ps->data[end];end--;}ps->data[pos] = x;ps->size++;
}	//中间删除数据的函数
void SeqListErase(SL* ps,int pos)
{int begin = pos;assert(ps != NULL);assert(pos >= 0);assert(pos < ps->size);while(begin < ps->size){ps->data[begin] = ps->data[begin + 1];begin++;}ps->size--;
}//头插函数
void SLPushFront(SL* ps,SeqListType x)
{assert(ps != NULL);SeqListInsert(ps,0,x);
}//头删函数
void SLPopFront(SL* ps)
{assert(ps != NULL);SeqListErase(ps,0);
}//尾插函数
void SLPushBack(SL* ps,SeqListType x)
{assert(ps != NULL);SeqListInsert(ps,ps->size,x);
}//尾删函数
void SLPopBack(SL* ps)
{assert(ps != NULL);SeqListErase(ps,ps->size-1);
}//查找函数
int Find(SL* ps,SeqListType x,int pos)
{int i = 0;assert(ps != NULL);assert(pos >= 0);assert(pos < ps->size);for(i = 0; i < ps->size; i++){if(ps->data[i] == x){return i;}}return -1;
}

顺序表的动态版本就讲解到这里,关注点一点,下期更精彩。

相关内容

热门资讯

商务部:近期出台系列促进服务出... 中新经纬8月27日电 国新办27日就中国服务贸易发展和2025年服贸会筹备工作进展情况举行新闻发布会...
民生银行济宁邹城支行走进公共法... 电信网络诈骗手段花样翻新,守护好群众的“钱袋子”成为当务之急。近日,民生银行济宁邹城支行主动作为,走...
初生牛犊不怕虎 武科大一群本科... 团队合影留念 湖北日报全媒记者 张歆 通讯员卢衍泰 黄妙实习生 胡文婕 初生牛犊不怕虎,武汉科技大学...
虞书欣起诉两公司侵权 天眼查天眼风险信息显示,近日,虞书欣与愉悦区块(杭州)科技有限公司、杭州趣园科技有限公司新增开庭公告...
隐瞒入党前严重错误!惠州惠东县... 8月27日,惠州市纪委监委发布通报,惠东县中小企业发展事务中心原四级调研员沈其伟严重违纪违法被开除党...
回应美威胁 欧盟重申有权制定数... 在美国特朗普政府威胁对他国数字监管举措采取关税报复后,欧盟方面26日作出坚决回应,强调欧盟有权自主制...
郑州市委副书记吕挺琳,获提拔 “河南民政”消息,8月26日,河南省民政厅党组书记吕挺琳主持召开党组(扩大)会议,传达学习第十九次全...
国泰海通:看好政策鼓励、供给优... 瑞财经 严明会国泰海通证券研究指出,8月25日,国家新闻出版署官网发布2025年8月国产网络游戏审批...
乌军:特种部队突袭克里米亚 俄方称继续重创乌军,乌军对多地实施打击。 俄罗斯国防部26日通报说,俄各集团军部队的攻击继续对乌克兰...
安徽一钓鱼佬钓到一条1米长20... 据广德公安,近日,一位市民来到安徽广德市公安局桃州派出所求助,称其在钓鱼时意外钓起一只鳄鱼。为防止鳄...