Status DestroyList_Sq(SqList& L){
//初始條件:線性表L存在
//操作結果:銷毀線性表L.
if (!L.elem){
printf("線性表不存在.\n");
return FALSE;
} free(L.elem);
printf("線性表銷毀成功.\n");
return OK;
}
Status ClearList_Sq(SqList& L){
//初始條件:線性表L存在
//操作結果:將L重置為空表
if (!L.elem){
printf("線性表不存在.\n");
return FALSE;
} InitList_Sq(L);
return OK;
}
Status ListEmpty_Sq(SqList L){
//初始條件:線性表L存在.
//操作結果:如果L是空表,返回TRUE,否則返回FALSE
if (L.length == 0){
printf("L是空表.\n");
return TRUE;
}
else{
printf("L不是空表.\n");
return FALSE;
}
}
Status ListLength_Sq(SqList L){
//初始條件:線性表L存在.
//操作結果:返回L中數據元素個數
return L.length;
}
Status GetElem_Sq(SqList L, int i, ElemType& e){
//初始條件:線性表已存在1 ≤ i ≤ ListLength(L).
//操作結果:用e返回L中第i個數據元素的值
e = L.elem[i-1];
return OK;
}
Status compare (ElemType x, ElemType y){
if (x == y){
return TRUE;
}
else{
return FALSE;
}
} Status LocateElem_Sq(SqList L, ElemType e, Status(*compare) (ElemType, ElemType)){
//線性表L存在,compare()是數據元素判定函數.返回L中第1個與e滿足關系compare()的數據元素的位序.
//若這樣的元素不存在,則返回0;
int i = 1;//i的初值為第一個元素的位序
ElemType* p = L.elem;//p的初值為第一個元素的存儲位置(地址)
while (i <= L.length && !(*compare)(*p++, e))
++i;
if (i <= L.length)
return i;
else return FALSE;
}
int PriorElem_Sq(SqList L, ElemType cur_e, ElemType& pre_e){
//初始條件: 線性表L存在.
//操作結果: 若cur_e是L的數據元素,且不是第一個,則用pre_e返回他的前驅,否則操作失敗,pre_e無定義NULL.
int i = 1;
i = LocateElem_Sq(L, cur_e, compare);
if (i >1 && i <= L.length){ //符合條件
pre_e = L.elem[i-2];//減1是當前值,再減1是前驅值
}
else{
pre_e = 0;
return FALSE;
}
return OK;
}
Status NextElem_Sq(SqList L, ElemType cur_e, ElemType& next_e){
//初始條件: 線性表L存在.
//操作結果: 若cur_e是L的數據元素,且不是最后一個,則用next_e返回他的后繼,否則操作失敗,next_e無定義NULL.
int i = 1;
i = LocateElem_Sq(L, cur_e, compare);
if (i >=1 && i < L.length){ //符合條件
next_e = L.elem;//i在數組中就是他的后繼.
}
else{
next_e = 0;
return FALSE;
}
return OK;
}
Status ListInsert_Sq(SqList& L, int i, ElemType e){
//初始條件:線性表L存在,1 ≤ i ≤ ListLength(L)+1
//操作結果:在L中第i個位置之前插入新的數據元素e,L的長度加1
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length >= L.listsize){
ElemType* newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType));
Status ListDelete_Sq(SqList& L, int i, ElemType& e){
//初始條件:線性表L存在且非空,1 ≤ i ≤ ListLength(L)
//操作結果:刪除L的第i個數據元素,并用e返回其值,L的長度減1
if ((i<1) || (i > L.length))
return ERROR;
ElemType* p = NULL;
p = &(L.elem[i - 1]);
e = *p;
ElemType* q = NULL;
q = L.elem + L.length - 1;
for (++p; p <= q; ++p)
*(p - 1) = *p;
--L.length;
return OK;
}