尾部附加源碼 增加數據
Status ListInsert_Sq(SqList &L,int i,ElemType e){ if(L.length == LIST_INIT_SIZE || i<1||i>L.length+1){ return ERROR; } ElemType *q,*p; p = &(L.elem[i-1]); /**這個可以有多種方法實現**/ q = L.elem + L.length; for(q;q>p;q--){ *q = *(q-1); } L.elem[i-1] = e; L.length++;}刪除數據
Status ListDelete_Sq(SqList &L,int i,ElemType &e){ if(L.length == LIST_INIT_SIZE || i<1||i>L.length+1){ return ERROR; } ElemType *p,*q; p = &(L.elem[i-1]);//p為被刪除元素的位置 e = *p;//將被刪除元素的值給e q = L.elem + L.length -1;//表中最后一個元素 //這里從被刪除的元素開始將后面的元素依次向前移動 for(++p;p<=q;++p){ *(p-1) = *p; } //這里也可以換種寫法 // for(p;p<=q;++p){*p = *(p+1) } --L.length; return OK;}查找數據
int LocateElem_Sq(SqList L, ElemType &e){ for(int i=0;i<L.length;i++){ if(L.elem[i] == e){ return i+1; } } return 0;}獲取數據
Status GetElem_Sq(SqList L,int i,ElemType &e){ if(L.length == LIST_INIT_SIZE || i<1||i>L.length+1){ return ERROR; } e = L.elem[i-1]; return OK;}兩個表的交集
void Mix_Sq(SqList A,SqList B){ ElemType e; SqList C; InitList_Sq(C); int j = 0; for(int i = 0; i<GetLength_Sq(B);i++){ GetElem_Sq(B,i+1,e); if(LocateElem_Sq(A,e)) ListInsert_Sq(C,++j,e); } show(C);//此方法是展示結果 DestroyList_Sq(C);//此方法是刪除創建的順序表}兩個表的并集
void Differ_Sq(SqList A,SqList B){ SqList C; InitList_Sq(C); ElemType e; int j = 0; for(int i=0;i<GetLength_Sq(A);i++){ GetElem_Sq(A,i+1,e); if(!LocateElem_Sq(B,e)) ListInsert_Sq(C,++j,e); } show(C);//此方法是展示結果 DestroyList_Sq(C);//此方法是刪除創建的順序表}兩個表的差集
void Differ_Sq(SqList A,SqList B){ SqList C; InitList_Sq(C); ElemType e; int j = 0; for(int i=0;i<GetLength_Sq(A);i++){ GetElem_Sq(A,i+1,e); if(!LocateElem_Sq(B,e)) ListInsert_Sq(C,++j,e); } show(C);//此方法是展示結果 DestroyList_Sq(C);//此方法是刪除創建的順序表}相信大家都能看懂,不多說了,附上源碼。 點擊下載
新聞熱點
疑難解答