数据结构-两集合的差集
#include "stdio.h"
typedef struct {
int data[20];
int length;
} list;
int LocateElem(list L,int e){
for (int i = 0; i < L.length; ++i) {
if(L.data[i] == e){
return i+1;
}
}
return 0;
}
int ListDelete(list *L,int i){
if(i <0 || i>=L->length){
return 0;
}
for (int j = i; j < L->length -1; ++j) {
L->data[j] = L->data[j+1];
}
L->length--;
return 1;
}
void del(list *La,list Lb){
int elem;
for (int i = 0; i < La->length; ++i) {
elem = La->data[i];
if(LocateElem(Lb,elem)){
ListDelete(La,i);
i--;
}
}
}
int main(){
list La = {{1,2,3,4,5},5};
list Lb = {{3,5,6,7,8},5};
printf("求差前: \n");
printf("La的值: ");
for (int i = 0; i < La.length; ++i) {
printf("%d,",La.data[i]);
}
printf("\nLb的值: ");
for (int i = 0; i < Lb.length; ++i) {
printf("%d,",Lb.data[i]);
}
del(&La,Lb);
printf("\n求差后: ");
for (int i = 0; i < La.length; ++i) {
printf("%d,",La.data[i]);
}
return 0;
}
运行结果:
|