顺序表

来源:互联网 发布:传奇霸业数据汇总 编辑:程序博客网 时间:2024/04/18 18:46

#include<stdio.h>

#include<stdlib.h>

 

typedef int ElemType;

typedef ElemType *Triplet;

typedef int Status;

 

#define List_INIT_SIZE 10//初始容量   */

#define LISTINCREMENT 2//增量      */

#define OK 1;

#define FALSE 0;

#define TRUE 1;

 

typedef struct/*存储结构*/

{

    ElemType *elem;/*基地址     */

    int length;/*当前长度     */

    int listsize;/*当前分配容量   */

}SqList;

 

/*判断值是否相等*/

Status equal(ElemType c1,ElemType c2);

  /* 初始条件:顺序线性表L已存在,compare()是数据元素判定函数(满足为1,否则为0) */

   /* 操作结果:返回L中第1个与e满足关系compare()的数据元素的位序。 */

   /*           若这样的数据元素不存在,则返回值为0。算法2.6 */

int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType));

   /* 初始条件:顺序线性表L已存在,1iListLength(L) */

   /* 操作结果:用e返回L中第i个数据元素的值 */

Status GetElem(SqList L,int i,ElemType *e);

 /* 初始条件:顺序线性表L已存在,1iListLength(L)+1 */

   /* 操作结果:在L中第i个位置之前插入新的数据元素eL的长度加1 */

Status ListInsert(SqList *L,int i,ElemType e) ;/* 算法2.4 */

/* 将所有在线性表Lb中但不在La中的数据元素插入到La */

 void Union(SqList *La,SqList Lb); /* 算法2.1 */

 

 

 

Status equal(ElemType c1,ElemType c2)

{

    if(c1==c2)

    {

        return TRUE;

    }

    else

    {

        return FALSE;

    }

}

int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))

{

    ElemType *p;

    int i=1;

    p=L.elem;

    while(i<=L.length)

        ++i;

    if(i<=L.length&&!compare(*p++,e))

        return i;

    else

        return 0;

}

 

Status InitList(SqList *L)

{

    (*L).elem=(ElemType*)malloc(List_INIT_SIZE*sizeof(ElemType));

    (*L).length=0;

    (*L).listsize=List_INIT_SIZE;

    return OK;

}

 

void aa(SqList *L)

{

    int i;

    for(i=0;i<(*L).length;i++)

    {

        printf("%d/n",*(L->elem+i));

    }

}

Status ListInsert(SqList *L,int i,ElemType e)

{

    ElemType *newbase,*q,*p;

    q=(*L).elem+i-1;

    for(p=(*L).elem+(*L).length-1;p>=q;--p)

    {

        *(p+1)=*p;

    }

    *q=e;

    ++(*L).length;

    return OK;

}

int ListLength(SqList L)

{

    return L.length;

}

Status GetElem(SqList L,int i,ElemType *e)

{

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

    return OK;

}

void Union(SqList *La,SqList Lb)

{

    ElemType e;

    int La_len,Lb_len;

    int i;

    La_len=ListLength(*La);

    Lb_len=ListLength(Lb);

    for(i=1;i<=Lb_len;i++)

    {

        GetElem(Lb,i,&e);

        if(!LocateElem(*La,e,equal))

            ListInsert(La,++La_len,e);

    }

}

 

main()

{

    SqList La,Lb;

    Status i;

    int j;

    i=InitList(&La);

    if(i==1)

    {

 

            i=ListInsert(&La,1,9);

            i=ListInsert(&La,2,19);

            i=ListInsert(&La,3,5);

            i=ListInsert(&La,4,3);

            i=ListInsert(&La,5,6);

    }

    i=ListInsert(&La,2,10);

       printf("----------a----------");

    aa(&La);

    i=InitList(&Lb);

    if(i==1)

    {

            i=ListInsert(&Lb,1,9);

            i=ListInsert(&Lb,2,50);

            i=ListInsert(&Lb,3,5);

    }

       printf("----------b----------");

    aa(&Lb);

              printf("----------a----------");

    Union(&La,Lb);

       aa(&La);

}