初始化 InitList (L) 操作前提:L为未初始化线性表 操作结果:将L初始化为空表 Seqlist l InitList(&L) void Initlist( seqlist "pl)i pL>last=-1
void InitList ( SeqList *pL ) { pL->last = -1; } 初始化 InitList(L 操作前提: L为未初始化线性表。 操作结果: 将L初始化为空表。 SeqList L; InitList(&L
查找 按序号查找:取线性表中第个元素,若存在 则返回该元素值,否则返回-1 ElemType GetData( Seqlist pL, int i) if( i>0&& K<=pL->last+1) return pL->elemi-1 else return -1
▪ 按序号查找:取线性表中第i个元素,若存在 则返回该元素值,否则返回-1 ElemType GetData ( SeqList *pL, int i ) { if( i>0 && i<=pL->last+1) return pL->elem[i-1]; else return -1; } 查找
查找 按值查找:找e在表中的位置,若查找成功,返回表 项的位置,否则返回-1 int Locate( Seq list *pL, ElemType e) inti=0 while(i<= pl->last &&e pl>elemi!=e) i+十 if (i<= pL->last return else return -1
▪ 按值查找:找 e 在表中的位置,若查找成功,返回表 项的位置,否则返回-1 int Locate ( SeqList *pL, ElemType e ) { int i = 0; while ( i <= pL->last && pL->elem[i] != e ) i++; if ( i <= pL->last ) return i; else return -1; } 查找
按值查找:判断e是否在表中 int IsIn( Seqlist *pL, ElemType e) int i=0. found=0 while (i<= pl->last &&.found if(pL->elemi!=e) i++; ese found=1 return found
按值查找:判断e是否在表中 int IsIn ( SeqList *pL, ElemType e ) { int i = 0, found=0; while ( i <= pL->last && !found ) if(pL->elem[i] != e ) i++; else found=1; return found; }
按值查找:寻找x的后继 int Next( SeqList"pl L ElemType int i=Locate(pL, x); if (i>=0 &&i< pL->last )return i+1; else return -1 寻找x的前驱 int Precursor( Seqlist*pL, Elem Type x) int i=Locate(pL, X) if (i>0&&i<= pL->last )return i-l else return -1
▪ 按值查找:寻找x的后继 int Next ( SeqList *pL, ElemType x ) { int i = Locate(pL, x); if ( i >=0 && i < pL->last ) return i+1; else return -1; } ▪ 寻找x的前驱 int Precursor ( SeqList *pL, ElemType x ) { int i = Locate(pL, x); if ( i >0 && i <= pL->last ) return i-1; else return -1; }