集合异或运算--记录学习C语言每一天

发布时间 2023-12-07 11:47:18作者: Rabbit_XIN
//
//  main.c
//  Hello
//
//  Created by renxin on 2023/11/28.
//
#define ElemType int
#define MaxSize 50


#include <stdio.h>
#include <stdlib.h>

typedef struct List{
    ElemType List[MaxSize];
    int Length;
    int Size;
}List;

void InitList(List *L, int size){
    L->Size = size;
    L->Length = 0;
}

void VisitList(List L){
    for(int i = 0;i < L.Length; ++i){
        printf("%d ",L.List[i]);
    }
    printf("\n");
}

int GetElem(List L,int i){
    return L.List[i];
}


void InsertELem(List *L, int i,int e){
    if(i > L->Length){
        printf("can not insert\n");
        return;
    }
    for (int j = L->Length; j > i; --j) {
        L->List[j] = L->List[j-1];
    }
    L->List[i] = e;
    ++L->Length;
    return;
}

int getElem(List L,int i){
    return L.List[i];
}

int searchElem(List L,int e){
    for (int i = 0; i < L.Length; ++i) {
        if(e == L.List[i]){
            return i;;
        }
    }
    return -1;
}

void deleteElem(List *L,int i){
    for (; i < L->Length-1; ++i) {
        L->List[i] =L->List[i+1];
    }
    --L->Length;
}
void caculate(List* La,List Lb){
    int e,t;
    for (int i = 0; i < Lb.Length; ++i) {
        e = getElem(Lb,i);
        if((t=searchElem(*La, e)) != -1){
            deleteElem(La,t);
 //           VisitList(*La);
        }else{
            InsertELem(La, 0, e);
 //           VisitList(*La);
        }
    }
}
int main(int argc, const char * argv[]) {
    List* La = (List*)malloc(sizeof(List));
    List* Lb = (List*)malloc(sizeof(List));
    int n,number;
    InitList(La, 10);
    InitList(Lb, 10);
    printf("Input La elem number:");
    scanf("%d",&n);
    for (int i = 0; i < n; ++i) {
        scanf("%d",&number);
        InsertELem(La, i, number);
    }
    printf("Input Lb elem number:");
    scanf("%d",&n);
    for (int i = 0; i < n; ++i) {
        scanf("%d",&number);
        InsertELem(Lb, i, number);
    }
    caculate(La, *Lb);
    VisitList(*La);
    return 0;
}