A,B,C为递增有序线性表,删去A中即在B出现也在C出现的元素

A,B,C为递增有序线性表,删去A中即在B出现也在C出现的元素

代码如下:
#include
#include <stdlib.h>

using namespace std;

const int MAX = 100;
typedef int ElemType;
struct SqList{
ElemType elem[MAX];
int Len;
};

void initList(SqList &L){
int x, len;
len = 0;
while(cin>>x && x!=-1){
L.elem[len++] = x;
}
L.Len = len;
}

//删除顺序表元素
void Delete(SqList &L, int i){
int j;
for(j=i+1; j<L.Len; j++)
L.elem[j-1] = L.elem[j];
L.Len–;
}

//删除B、C交集元素
void deleteIntersection(SqList &A, SqList B, SqList C){
int i, j, k;
i = j = k = 0;
while(j<B.Len && k<C.Len){
if(B.elem[j] == C.elem[k]){
while(i<A.Len && A.elem[i]<=B.elem[j]){
if(A.elem[i] == B.elem[j]){
int t;
for(t=i+1; t<A.Len; t++)
A.elem[t-1] = A.elem[t];
A.Len–;
}else{
i++;
}
}
j++;
k++;
}else if(B.elem[j] > C.elem[k]){
k++;
}else if(B.elem[j] < C.elem[k]){
j++;
}
}
}

void Output(SqList L){
int i;
for(i=0; i<L.Len; i++)
cout << L.elem[i] << " ";
cout << endl;
}
int main(){
SqList A, B, C;
cout << “输入表A中元素:”;
initList(A);
cout << “输入表B中元素:”;
initList(B);
cout << “输入表C中元素:”;
initList©;
cout << “删除前的表A:”;
Output(A);
deleteIntersection(A, B, C);
cout << “删除后的表A:”;
Output(A);
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51732593/article/details/121842606