单链表实现交差和

#define MAXSIZE 10
#define ADDSIZE 5
typedef int ElemType;

typedef struct{
    ElemType *elem;
    int length;
    int size;
}SqList;
<pre name="code" class="cpp">#include "stdio.h"
#include "malloc.h"
#include"head.h"

void InitList(SqList *L){
    L->elem=(ElemType *)malloc(MAXSIZE * sizeof(ElemType));
    if(L!=NULL){
        (*L).length=0;
        (*L).size=MAXSIZE;
        printf("内存分配成功!\n");
     }else{
         printf("内存分配失败!\n");
     }
}
int ListInsert(SqList *L,int i,ElemType e){
    int j;
    if(i<1||i>(*L).length+1){
        ElemType * newElem;
        int k;
        newElem = (ElemType *)malloc((L->size+ADDSIZE)*sizeof(ElemType));
        for(k=0;k<L->length;k++){
            newElem[k]=L->elem[k];
        }
        L->elem = newElem;
        L->size=L->size+ADDSIZE;

    }
    i--;
    for(j=L->length;j>i;j--){
        L->elem[j]=L->elem[j-1];
    }
    L->elem[i]=e;
    L->length++;
    return 1;
}
void ListTraverse(SqList *L){
    int i;
    if(ListEmpty(L)){
        return ;
    }
    for(i=0;i<L->length;i++){
        printf("%d\t",L->elem[i]);
    }
    printf("\n");
}
int ListEmpty(SqList *L){
    return (L->length==0);
}
int ListLength(SqList *L){
    return (L->length);
}
int ListSize(SqList *L){
    return L->size;

}


ElemType GetElement(SqList *L,int i){
    ElemType e=0;
    if(i<1||i>L->length){
        return e;
    }
    e=L->elem[i-1];
    return e;

}
int LocateElement(SqList *L,int i){
    ElemType e=0;
    if(i<1||i>L->length){
        return e;
    }
    e=L->elem[i-1];
    return e;
}
void ClearList(SqList *L){
    L->length=0;
}
void DestroyList(SqList *L){
    free(L->elem);
    L->length=0;
    L->size=0;

}

int PutElement(SqList *L,int i,ElemType newValue){

    int j;
    for(j=0;j<L->length;j++){
        if(i==j+1){
            L->elem[j]=newValue;
            return 1;
        }
    }
    return 0;

}
int ListDelete(SqList *L,int i){
    int j;
    if(i<1||i>L->length){
        return 0;
    }
    i--;
    for(j=i;j<L->length-1;j++){//向后移动指针域
        L->elem[j]=L->elem[j+1];
    }
    L->length--;//总长度少一
    return 1;
}


 
<pre name="code" class="cpp">#include "body.c"


SqList * SetDifference(SqList *La,SqList *Lb,SqList *Lc){
    int i,j;
    ElemType e;
    for(i=1;i<=ListLength(La);i++){
        e=GetElement(La,i);
        for(j=1;j<=ListLength(Lb);j++){
            if(e==GetElement(Lb,j)){
                break;
            }else if(j==ListLength(Lb)){
                ListInsert(Lc,ListLength(Lc)+1,e);
            }
        }
    }
    return Lc;
}
SqList * SetJiao(SqList *La,SqList *Lb,SqList *Lc){
    int i,j;
    ElemType e;
    for(i=1;i<=ListLength(La);i++){
        e=GetElement(La,i);
        for(j=1;j<=ListLength(Lb);j++){
            if(e==GetElement(Lb,j)){
                ListInsert(Lc,ListLength(Lc)+1,e);
            }
        }
    }

    return Lc;
}
SqList * SetHe(SqList *La,SqList *Lb,SqList *Lc){
    int i,j;
    ElemType e;

    for(i=1;i<=ListLength(La);i++){
             e=GetElement(La,i);
        ListInsert(Lc,ListLength(Lc)+1,e);
    }
    for(i=1;i<=ListLength(Lb);i++){
         e=GetElement(La,i);
        ListInsert(Lc,ListLength(Lc)+1,e);
    }
    return Lc;
}

main(){
    SqList La;
    SqList Lb;
    SqList Lc,*lc,*lc1,*lc2;
    SqList Le,*le;
    int i;
    InitList(&La);
    InitList(&Lb);
    InitList(&Lc);
    InitList(&Le);

    for(i=1;i<=10;i++){
        ListInsert(&La,i,i);
    }
    for(i=1;i<=5;i++){
        ListInsert(&Lb,i,i);
    }


    printf("La=");
    ListTraverse(&La);
    printf("Lb=");
    ListTraverse(&Lb);

    lc = SetDifference(&La,&Lb,&Lc);
    printf("²î=");
    ListTraverse(lc);

    ClearList(lc);
   lc1 = SetJiao(&La,&Lb,&Lc);
   printf("½»=");
    ListTraverse(lc1);

    ClearList(lc1);
    lc2 = SetHe(&La,&Lb,&Lc);
    printf("ºÍ=");
   ListTraverse(lc2);
ClearList(lc2);

    le = SetDifference(SetHe(&La,&Lb,&Lc),SetJiao(&La,&Lb,&Lc),&Le);
    printf("²¢=");
    ListTraverse(le);

}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值