顺序表的初始化插入删除等基本操作算法C语言

上传人:沈*** 文档编号:61878521 上传时间:2022-03-13 格式:DOC 页数:10 大小:59.50KB
收藏 版权申诉 举报 下载
顺序表的初始化插入删除等基本操作算法C语言_第1页
第1页 / 共10页
顺序表的初始化插入删除等基本操作算法C语言_第2页
第2页 / 共10页
顺序表的初始化插入删除等基本操作算法C语言_第3页
第3页 / 共10页
资源描述:

《顺序表的初始化插入删除等基本操作算法C语言》由会员分享,可在线阅读,更多相关《顺序表的初始化插入删除等基本操作算法C语言(10页珍藏版)》请在装配图网上搜索。

1、/* Linear Table On Sequence Structure */#include #include#include #include /*-*/typedef int status;#define LISTINCREMENT 10#define LIST_INIT_SIZE 100#define TRUE 1#define FALSE 0#define OVERFLOW -2#define ERROR 0#define OK 1typedef structint item1;Elemtype;typedef structElemtype * elem;int length;in

2、t listsize;SqList;/*-*/status Getlist_One(SqList *L);status SaveData(SqList * L);status InitList_One(SqList *L);status IntiaList(SqList * L);status DestroyList(SqList * L);status ClearList(SqList* L);status ListEmpty(SqList L);int ListLength(SqList L);status GetElem(SqList L,int i,Elemtype * e);stat

3、us LocatElem(SqList L,Elemtype e);/*status PriorElem(SqList L,Elemtype cur,Elemtype * pre_e);status NextElem(SqList L,Elemtype cur,Elemtype * next_e);*/status ListInsert(SqList * L,status i, Elemtype e);status ListDelete(SqList * L);status ListTrabverse(SqList L,void (* visit)(Elemtype e);/*-*/statu

4、s equal(Elemtype x, Elemtype y);void display(Elemtype e);/*-*/void menu(void);/*-*/void main(void)SqList L1,L2;int op=0;L1.elem=L2.elem=NULL;char*L1_info_filename=L1.dat;InitList_One(&L1);/*L1.elem=(Elemtype *) malloc(sizeof(Elemtype)*10);L1.length=4;L1.elem0.item1=1;L1.elem1.item1=2;L1.elem2.item1=

5、3;L1.elem3.item1=4;*/do /clrscr(); menu(); printf( Please input your option0-12:); scanf(%d,&op); switch(op) case 0: break; case 1: printf(n here is IntiaList(),which being realizedn); if(IntiaList(&L1)=OK) printf(n 初始化成功n); else printf( 初始化失败n); getchar();getchar(); break; case 2: printf(n here is

6、DestroyList(),which being realizedn); DestroyList(&L1); if(L1.elem=NULL) printf(n 销毁成功n); else printf(n 销毁失败n); getchar();getchar(); break; case 3: printf(n here is ClearList(),which being realizedn); ClearList(&L1); if(L1.length=0) printf(n 置空成功n); else printf(n 置空失败n); getchar();getchar(); break;

7、case 4: printf(n here is ListEmpty(),which being realizedn); if(ListEmpty(L1)=TRUE) printf(n 该表为空n); else printf(n 该表不为空n); getchar();getchar(); break; case 5: printf(n here is ListLength() ,which being realizedn); int i5; i5=ListLength(L1); printf( 表长为:%dn,i5); getchar();getchar(); break; case 6: p

8、rintf(n here is GetElem(),which being realizedn); int i6; Elemtype e6; printf( 输入需得到元素的序号:); scanf(%d,&i6); GetElem(L1,i6,&e6); printf( 第%d个元素的值为:%dn,i6,e6.item1); getchar();getchar(); break; case 7: printf(n here is LocatElem(),which being realizedn); Elemtype a; printf( 输入需寻找的元素的值:); scanf(%d,&a.i

9、tem1); if(LocatElem(L1,a)=TRUE) printf(n 线性表中存在该元素。n); else printf(n 线性表中不存在该元素。n); getchar();getchar(); break; case 8: printf(n here is PriorElem(),which being realizedn); getchar();getchar(); break; case 9: printf(n here is NextElem(),which being realizedn); getchar();getchar(); break; case 10: pr

10、intf(n here is ListInsert(),which being realizedn); int i10; Elemtype e10; printf(输入插入元素的值:); scanf(%d,&e10.item1); printf(输入插入元素的插入位置:); scanf(%d,&i10); ListInsert(&L1,i10,e10);getchar();/getchar();break; case 11: printf(n here is ListDelete(),which being realizedn); ListDelete(&L1);break; case 12:

11、 /*printf(n here is ListTrabverse(),which being realizedn);*/ char tablename9; doprintf(n please specify liear table nameL1,L2: );scanf(%s,tablename); while(strcmp(tablename,L1) & strcmp(tablename,L2); if(!strcmp(tablename,L1) if(!L1.elem) printf(n liear table L1 does not exist!n); else if(!ListTrab

12、verse(L1,display)printf(n this liear table is empty!n); if(!strcmp(tablename,L2) if(!L2.elem) printf(n liear table L2 does not exist!n); else if(!ListTrabverse(L2,display)printf(n this liear table is empty!n); getchar();getchar(); break; default: ; while(op);printf(n-Welcome again!-n);getchar();getc

13、har();/顺序表初始化 status InitList_One(SqList *L)L-elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype);if (!L-elem)exit(ERROR);/ 存储分配失败L-length = 0;/ 空表长度为0L-listsize = LIST_INIT_SIZE;int i=Getlist_One(L);/调用下面的函数if(!i)L-elem=NULL;return OK;/从文件中读取顺序表 status Getlist_One(SqList *L)int i = 0;int data

14、;FILE *fp;if(!(fp=fopen(n L1.dat,rb) printf(n L1 未被初始化!n); getchar(); return ERROR; while(fscanf(fp,%d,&data)!=EOF)L-length+;L-elemi.item1 = data;i+;if(L-length=L-listsize) /如果线性表的长度大于等于分配的大小时,需重新分配线性表大小Elemtype *newbase=(Elemtype *)realloc(L-elem ,(L-length +LISTINCREMENT)*sizeof(Elemtype);if (!new

15、base)return ERROR;L-elem =newbase; /基地址改变L-listsize +=LISTINCREMENT; /分配的大小加倍fclose(fp);return OK;status SaveData(SqList *L)FILE *fp;int i;char*filename=L1.dat;fp=fopen(filename,wb);for(i=0;ilength;i+)fprintf(fp,%d ,L-elemi.item1);fclose(fp);return OK;status IntiaList(SqList * L) L-elem=(Elemtype*)m

16、alloc(LIST_INIT_SIZE*sizeof(Elemtype); if(!L-elem)exit(OVERFLOW); L-length=0; L-listsize=LIST_INIT_SIZE; return OK;status DestroyList(SqList * L) free(L);status ClearList(SqList* L) L-length=0;status ListEmpty(SqList L) if(L.length=0) return TRUE; else return FALSE;int ListLength(SqList L) return(L.

17、length);status GetElem(SqList L,int i,Elemtype * e) if(iL.length)return 0; e-item1=L.elemi-1.item1; return 1;status LocatElem(SqList L,Elemtype e) int i=0; if(L.elem=NULL)return FALSE; while(iL.length) if(L.elemi.item1=e.item1)return TRUE; +i; if(i=L.length) return FALSE;/*status PriorElem(SqList L,

18、Elemtype cur,Elemtype * pre_e)status NextElem(SqList L,Elemtype cur,Elemtype * next_e)*/status ListInsert(SqList * L,status i, Elemtype e) int j; if (iL-length+1) /i的合法取值为1至n+1 return ERROR; if (L-length=L-listsize) /*溢出时扩充*/ Elemtype *newbase; newbase=(Elemtype *) realloc(L-elem, (L-listsize+LISTIN

19、CREMENT)*sizeof(Elemtype); if (newbase=NULL) return OVERFLOW; /扩充失败 L-elem=newbase; L-listsize+=LISTINCREMENT; for(j=L-length-1;j=i-1;j-) L-elemj+1=L-elemj; L-elemi-1=e; L-length+; return OK;status ListDelete(SqList * L) if(L-elem=NULL) printf(此顺序表未被初始化!); getchar();getchar(); return -1; if(!L-lengt

20、h) printf(顺序表是空表,没有可删除的元素.n); getchar();getchar(); return(0); int i,j;Elemtype e;printf(要删除第几个元素:n);do printf(请在1%d之间输入!n,L-length); scanf(%d,&i); while(iL-length);e=L-elemi-1;printf(想要删除的元素中的整数是 %d?n,e.item1);getchar(); getchar();for(j=i-1;jlength-1;j+)L-elemj=L-elemj+1;L-length-;/if(!SaveData(L)re

21、turn ERROR;printf(该元素成功从顺序表中删除 !n);getchar();getchar();return OK;status ListTrabverse(SqList L, void (* visit)(Elemtype e)int i;if(!L.length) return(0);printf(n-all elements of liear table-n);for(i=0;iL.length;i+) visit(L.elemi);return(1);/*-*/void menu(void)printf(nn);printf( Menu for Linear Table

22、On Sequence Structure n);printf(-n);printf( 1. IntiaList 7. LocatElemn);printf( 2. DestroyList 8. PriorElemn);printf( 3. ClearList 9. NextElem n);printf( 4. ListEmpty 10. ListInsertn);printf( 5. ListLength 11. ListDeleten);printf( 6. GetElem 12. ListTrabversen);printf( 0. Exitn);printf(-n);/*-*/status equal(Elemtype x, Elemtype y)return (x.item1=y.item1);void display(Elemtype e)printf(%d ,e.item1);

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!