资源描述:
《数据结构线性表基本操作(c语言)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;typedefintElemType;#defineLIST_INIT_SIZE100#defineLISTINCREMENT10typedefstruct{ElemType*elem;intlength;intlistsize;}SqList;Statu
2、sInitList_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,Sta
3、tus(*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位插入新的元素eStatusList
4、Delete_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("构造失败!");exit(OVERFLOW);}L->length=0;L->listsize=LIST_INIT_SIZE;printf("构造成功!");returnOK;}voidDestroyList_
5、Sq(SqList*L)//销毁一个线性表{if(L->elem!=NULL){free(L->elem);L->elem=NULL;L->length=0;L->listsize=0;printf("已销毁线性表!");}}voidClearList_Sq(SqList*L)//将L置为空表{if(L->elem!=NULL){L->length=0;printf("已将L置为空表!");}}StatusListEmpty_Sq(SqListL)//空表返回TRUE{if(L.elem!=NULL){if(L.length==0){printf("
6、是空表");returnTRUE;}else{printf("不是空表");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("为空表!");returnERROR;}if(i<1
7、
8、
9、i>L.length){printf("不存在地%d个位置!",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{
10、return0;}}/*指向函数的指针*/Statu