您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 数据结构线性表基本操作(C语言)
#includestdio.h#includestdlib.h#includeDefine.h#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;typedefintElemType;#defineLIST_INIT_SIZE100#defineLISTINCREMENT10typedefstruct{ElemType*elem;intlength;intlistsize;}SqList;StatusInitList_Sq(SqList*L);//构造空的线性表voidDestroyList_Sq(SqList*L);//销毁一个线性表voidClearList_Sq(SqList*L);//将L置为空表StatusListEmpty_Sq(SqListL);//空表返回TRUEStatusListLength_Sq(SqListL);//返回元素个数StatusGetElem_Sq(SqListL,inti,ElemType*e);//用e返回第i个元素算法2.2中使用StatusLocateElem_Sq(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType));//在L中找到一个值与e满足compare()的元素的位序StatusPriorElem_Sq(SqListL,ElemTypecur_e,ElemType*pre_e);//用pre_e返回cur_e的前驱StatusNextElem_Sq(SqListL,ElemTypecur_e,ElemType*next_e);//用next_e返回cur_e的后继StatusListInsert_Sq(SqList*L,inti,ElemTypee);//在第i位插入新的元素eStatusListDelete_Sq(SqList*L,inti,ElemType*e);//删除第i个元素用e返回//算法2.3StatusInitList_Sq(SqList*L)//构造空的线性表{L-elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if(!L-elem){printf(构造失败!\n);exit(OVERFLOW);}L-length=0;L-listsize=LIST_INIT_SIZE;printf(构造成功!\n);returnOK;}voidDestroyList_Sq(SqList*L)//销毁一个线性表{if(L-elem!=NULL){free(L-elem);L-elem=NULL;L-length=0;L-listsize=0;printf(已销毁线性表!\n);}}voidClearList_Sq(SqList*L)//将L置为空表{if(L-elem!=NULL){L-length=0;printf(已将L置为空表!\n);}}StatusListEmpty_Sq(SqListL)//空表返回TRUE{if(L.elem!=NULL){if(L.length==0){printf(是空表\n);returnTRUE;}else{printf(不是空表\n);returnFALSE;}}else{exit(ERROR);}}StatusListLength_Sq(SqListL)//返回元素个数{if(L.elem!=NULL){returnL.length;}else{returnERROR;}}StatusGetElem_Sq(SqListL,inti,ElemType*e)//用e返回第i个元素算法2.2中使用{if(ListEmpty_Sq(L)){printf(为空表!\n);returnERROR;}if(i1||iL.length){printf(不存在地%d个位置!\n,i);returnERROR;}*e=L.elem[i-1];returnOK;}//算法2.6StatusLocateElem_Sq(SqListL,ElemTypee,Status(*compare)(ElemType,ElemType))//在L中找到一个值与e满足compare()的元素的位序{inti=1;int*p=L.elem;while(i=L.length&&!(*compare)(*p++,e)){++i;}if(i=L.length){returni;}else{return0;}}/*指向函数的指针*/StatusPriorElem_Sq(SqListL,ElemTypecur_e,ElemType*pre_e)//用pre_e返回cur_e的前驱{inti=2;while(i=L.length){if(cur_e==L.elem[i-1]){*pre_e=L.elem[i-2];returnOK;}i++;}returnERROR;}StatusNextElem_Sq(SqListL,ElemTypecur_e,ElemType*next_e)//用next_e返回cur_e的后继{inti=1;while(iL.length){if(cur_e==L.elem[i-1]){*next_e=L.elem[i];returnOK;}i++;}returnERROR;}//算法2.4StatusListInsert_Sq(SqList*L,inti,ElemTypee)//在第i位插入新的元素e{ElemType*newbase,*p,*q;if(i1||iL-length+1){returnERROR;}if(L-length=L-listsize){newbase=(ElemType*)realloc(L-elem,(L-listsize+LISTINCREMENT)*sizeof(ElemType));if(!newbase){exit(OVERFLOW);}L-elem=newbase;L-listsize+=LISTINCREMENT;}q=&(L-elem[i-1]);for(p=&(L-elem[L-length-1]);p=q;p--){*(p+1)=*p;}*q=e;++L-length;returnOK;}//算法2.5StatusListDelete_Sq(SqList*L,inti,ElemType*e)//删除第i个元素用e返回{ElemType*p,*q;if(i1||iL-length){returnERROR;}p=&(L-elem[i-1]);*e=*p;q=L-elem+L-length-1;for(++p;p=q;p++){*(p-1)=*p;}--L-length;returnOK;}Statusmain(void){SqListL;ElemTypei,n=0,e=0,cur_e=0,pre_e=0,next_e=0;charch;printf(初始化线性表···);InitList_Sq(&L);printf(是否销毁线性表L?'Y'OR'N');ch=getchar();if(ch=='Y'){DestroyList_Sq(&L);return0;}else{ClearList_Sq(&L);}for(i=1;i=LISTINCREMENT;i++){L.elem[i-1]=i;L.length++;}printf(线性表内初始数值为:\n);for(i=1;i=LISTINCREMENT;i++){printf(%4d,L.elem[i-1]);}printf(\n);n=ListLength_Sq(L);printf(线性表内元素个数为%3d\n,n);printf(欲知道第i位数字i=);scanf(%d,&i);GetElem_Sq(L,i,&e);printf(第%d位数字为%d\n,i,e);cur_e=e;PriorElem_Sq(L,cur_e,&pre_e);printf(%d的前驱是%d\n,cur_e,pre_e);NextElem_Sq(L,cur_e,&next_e);printf(%d的后继是%d\n,cur_e,next_e);printf(请输入要插入的位数和要插入的数字:);scanf(%d%d,&n,&e);ListInsert_Sq(&L,n,e);printf(插入后线性表内%d个数据为:\n,L.length);for(i=1;i=L.length;i++){printf(%4d,L.elem[i-1]);}printf(\n);ListDelete_Sq(&L,n,&e);printf(删除线性表中第%d个数据%d后,线性内%d个数据为:\n,n,e,L.length);for(i=1;i=L.length;i++){printf(%4d,L.elem[i-1]);}printf(\n);return0;}
本文标题:数据结构线性表基本操作(C语言)
链接地址:https://www.777doc.com/doc-4696766 .html