单链表实现交差和

来源:互联网 发布:nba2k17欧文捏脸数据 编辑:程序博客网 时间:2024/06/05 09:53
#define MAXSIZE 10#define ADDSIZE 5typedef 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);}



0 0
原创粉丝点击