数据结构算法实现:利用两个线性表LA和LB分别表示两个集合A和B,现要求一个新的集合A=A并B.

1个回答

  • #include

    #include

    #include

    typedef int status;

    typedef int ElemType;

    #define TRUE 1

    #define ERROR 0

    #define FALSE 0

    #define OK 1

    #define OVERFLOW -2

    #define list_init_size 100

    #define listincrement 10

    typedef struct{

    x05ElemType *elem;

    x05int length;

    x05int listsize;

    }sqlist;

    status equal(ElemType a,ElemType b)

    {if(a==b)

    return TRUE;

    else

    return FALSE;

    }

    int listlength(sqlist l)

    { return l.length;}

    status listinsert(sqlist *l,int i,ElemType e)

    {

    x05ElemType *newbase,*q,*p;

    x05if(i(*l).length+1)

    x05return ERROR;

    x05if((*l).length>=(*l).listsize){

    x05x05newbase=(ElemType*)realloc((*l).elem,((*l).listsize+listincrement)*sizeof(ElemType));

    if(!newbase) exit(OVERFLOW);

    x05 (*l).elem=newbase;

    x05 (*l).listsize+=listincrement;

    }

    q=&((*l).elem[i-1]);

    for(p=&((*l).elem[(*l).length-1]);p>=q;--p)

    x05 *(p+1)=*p;

    *q=e;

    ++(*l).length;

    return OK;

    }

    status initlist(sqlist *l){

    x05(*l).elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));

    x05if(!(*l).elem)

    x05x05exit(OVERFLOW);

    x05(*l).length=0;

    x05(*l).listsize=list_init_size;

    x05return OK;

    }

    status getelem(sqlist l,int i,ElemType *e)

    { if(il.length)

    exit(ERROR);

    *e=*(l.elem+i-1);

    return OK;

    }

    int LocateElem(sqlist L,ElemType e,status(*compare)(ElemType,ElemType))

    {

    ElemType *p;

    int i=1;

    p=L.elem;

    while(i