




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/*1. 设计线性表顺序存储结构的11个基本操作函数,并编程实现之 */#include<stdio.h>#include<stdlib.h>#include<string.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef char ElemType;typedef struct ElemType *elem;int length;int listsize;SqList;bool compare(ElemType p,ElemType e)if (p=e) return true;else
2、 return false;void InitList(SqList &L)/构造一个空的线性表LL.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(!L.elem)exit(0);L.length=0;L.listsize=LIST_INIT_SIZE; void DestroyList(SqList &L)/销毁线性表Lfree(L.elem);L.elem=NULL;if(L.elem)exit(0);L.length=0;void ClearList(SqList &L)/将L重置为空表L.el
3、em='0'L.length=0;bool ListEmpty(SqList &L)/若L为空表,返回TRUE,否则FALSEif(L.length=0)return true;else return false;int ListLength(SqList L)/返回L中元素个数return L.length;bool GetElem(SqList L,int i,ElemType &e)/用e返回数据中第i个元素的值if(!L.length) return false;elseif(i>L.length|i<1) return false;else
4、e=L.elemi-1;return true;int LocateElem(SqList L,ElemType e)int i=1;bool cmp=0;if(L.length=0) return 0;elsewhile(i<=L.length&&!cmp)cmp=compare(L.elemi-1,e);i+;if(cmp) return i-1;else return 0;int PriorElem(SqList L,ElemType cur_e,ElemType &pre_e)/若cur_e是L的数据元素,且不是第一个,用pre_e返回它的前驱。否则操作失
5、败int i=LocateElem(L,cur_e);if(i=0) printf("不存在您定位的元素!n");else if(i=1) printf("您指定的是首元素,无前驱!n");else pre_e=L.elemi-2;return i;int NextElem(SqList L,ElemType cur_e,ElemType &next_e)int i=LocateElem(L,cur_e);if(i=0) printf("不存在您定位的元素!n");else if(i=L.length) printf(&quo
6、t;您指定的是末元素,无后继!n");next_e=L.elemi;return i;bool ListInsert(SqList &L,int i,ElemType e)/在L的第i个位置之前插入新的数据e,L的长度增加1ElemType *newbase,*p,*q;if(i<1|i>L.length+1)return false;elseif(L.length>=L.listsize)newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType);if(!new
7、base) exit(0);L.elem=newbase;L.listsize+=LISTINCREMENT;q=&(L.elemi-1);for(p=&(L.elemL.length-1);p>=q;-p) *(p+1)=*p;*q=e;L.elemL.length+1='0'+L.length;return true;bool ListDelete(SqList &L,int i,ElemType &e)/删除第i个元素,并用e返回,长度减1ElemType *p,*q;if(i<1|i>L.length)return fa
8、lse;elsep=&(L.elemi-1);e=*p;q=L.elem+L.length-1;for(+p;p<=q;+p) *(p-1)=*p;L.elemL.length-1='0'-L.length;return true;void read_file(SqList &L,ElemType FileName)FILE *fp_in;InitList(L);if(fp_in=fopen(FileName,"r")=NULL)printf("can not open file!n");exit(0);while(
9、!feof(fp_in) fgets(L.elem,LIST_INIT_SIZE,fp_in);L.length=strlen(L.elem);fclose(fp_in);void write_file(SqList L)printf("%sn",L.elem);FILE *fp_out;if(fp_out=fopen("re_data.txt","w")=NULL)printf("can not open file!n");exit(0);fputs(L.elem,fp_out);fclose(fp_out);v
10、oid menu()printf("nn");printf(" 请选择您需要的操作:nn");printf("*n");printf("| 0.ShowList 显示L中所有元素 |n");printf("| 1.ClearList 将L重置为空表 |n");printf("| 2.ListEmpty 检查L为是否为空表 |n");printf("| 3.ListLength 返回L中元素个数 |n");printf("| 4.GetElem 用
11、e返回表中第i个元素的值 |n");printf("| 5.LocateElem 找到元素e在表中的位 |n");printf("| 6.PriorElem 用pre_e返回它的前驱 |n");printf("| 7.NextElem 用next_e返回它的后继 |n");printf("| 8.ListInsert 在L第i个位置前插入数据e |n");printf("| 9.ListDelete 删除第i个元素,并用e返回 |n");printf("| 10.Destro
12、yList 销毁线性表L |n");printf("| 11.exit 返回 |n"); printf("*n");printf("n");int main()int choice;int select;int locate;ElemType e,pre_e,next_e;ElemType FileName12;SqList list;doprintf("nn1.读取默认文件(data.txt)n2.读取您指定文件n3.退出nnn");printf("请输入您的选择:");scanf(
13、"%d",&select);if(select=1) strcpy(FileName,"data.txt");else if(select=2)printf("请输入文件名:n");getchar();gets(FileName);else if(select!=3) printf("请输入正确选项!n");continue;read_file(list,FileName);printf("已读取您指定文件的顺序表L!n");system("cls");/domenu
14、();scanf("%d",&choice);switch(choice)case 0:system("cls");printf("以下是顺序表的所有元素:n%sn",list.elem);break;case 1:system("cls");ClearList(list);printf("顺序表已清空!n");break;case 2:system("cls");if(ListEmpty(list) printf("L为空表!n");else p
15、rintf("L非空!n");break;case 3:system("cls");printf("L的长度为:%dn",ListLength(list);break;case 4:system("cls");printf("选择您要读的元素位置:n");scanf("%d",&locate);if(GetElem(list,locate,e) printf("第%d个元素是:%cn",locate,e);else printf("顺序表
16、长度为%d,输入位置不正确!",ListLength(list);break;case 5:system("cls");printf("请输入您要定位的元素:n");getchar();e=getchar();locate=LocateElem(list,e);if(locate) printf("元素%c在L的第%d个位置上!n",e,locate);else printf("定位有误!n");break;case 6:system("cls");printf("请输入您要
17、定位的元素:n");getchar();e=getchar();locate=PriorElem(list,e,pre_e);if(locate>1)printf("您定位的元素%c的前驱元素为%cn",e,pre_e);else printf("定位有误!n");break;case 7:system("cls");printf("请输入您要定位的元素:n");getchar();e=getchar();locate=NextElem(list,e,next_e);if(locate<lis
18、t.length&&locate)printf("您定位的元素%c的后继元素为%cn",e,next_e);else printf("定位有误!n");break;case 8:system("cls");printf("请输入您指定的位置:n");scanf("%d",&locate);printf("请输入您要插入的元素:n");getchar();e=getchar();if(ListInsert(list,locate,e) printf("修改成功!n");else printf("您指定有误!n");break;case 9:system("cls");printf("请输入您指定的位置:n");scanf("%d",&locate);if(ListDelete(list,locate,e) printf("您删除的元素是%c:n",e);else printf("您指定有误!n"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络游戏虚拟物品交易安全认证与技术支持协议
- 农田水利设施灌溉用水权承包转让合同
- 生命科学企业细胞冻存服务及专用储存盒租赁合同
- 保险退保金结算与客户权益保障协议
- 微信小程序电商运营培训与客户关系管理协议
- DB42-T 2018-2023 大水面渔业资源调查评价技术规范
- 上海电子信息职业技术学院《农业相关政策培训》2023-2024学年第二学期期末试卷
- 江西工业职业技术学院《中西医结合重症医学》2023-2024学年第二学期期末试卷
- 四川省乐山市犍为县2025年初三下学期强化选填专练(二)生物试题含解析
- 江西现代职业技术学院《建筑史纲》2023-2024学年第一学期期末试卷
- 2024年安徽安庆安桐城乡发展集团有限公司招聘真题
- 拆除冷库施工方案
- 2025年九江市第一批面向社会公开招聘留置看护队员【68人】笔试备考题库及答案解析
- 2025-2030中国可再生能源行业发展分析及投资前景与战略规划研究报告
- 10.1 美国课件2024-2025学年度七年级下学期人教版地理
- 铆接粘接与锡焊教案
- 工业数字孪生测试要求
- 2025统编版语文六年级下册第二单元解析+任务目标+大单元教学设计
- 灾后救援与重建
- 上海第二工业大学《高等数学B(上)》2023-2024学年第二学期期末试卷
- 2025届上海市(春秋考)高考英语考纲词汇对照表清单
评论
0/150
提交评论