线性表数据结构C语言实现

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

线性表数据结构C语言实现



程序流程:

 

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#define OK 1

#define ERROR 0

#define OVERFLOW 0

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

 

typedef struct

{

    int *elem;

    int length;

    int listsize;

}SqList;

 

int InitList(SqList &L)

{

    L.elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));

    if(!L.elem)

       exit(OVERFLOW);

    L.length =0;

    L.listsize = LIST_INIT_SIZE;

    return OK;  

}

 

int CreateList(SqList &L)

    {

       int i;

       printf("Input the datas: ");

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

         scanf("%d",&L.elem[i]);

       return OK; 

    }

   

int OutputList(SqList L)

{

    int i;

    printf("Ouput the datas /n");

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

      printf("%d/t",L.elem[i]);

      printf("/n");

    return OK; 

}  

   

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

    {  

              int j,*p;

              j =1;

              p = L.elem;

              while(j <= L.length && !(*compare)(*p++,e))

              ++j;

              if(j<=L.length)

                 return j;

              else

                 return 0;  

    }

   

int compare(int a,int b)

    {

              if(a==b)

                 return 1;

              else

                 return 0;  

    }

   

int ListInsert(SqList &L,int i,int e)

    {

             

              int *p,*q,*newbase;

              if((i<1) || (i >L.length+1))

              return ERROR;

              if(L.length >= L.listsize)

                {

                     newbase = (int *)realloc(L.elem,(L.length + LISTINCREMENT)*sizeof(int));

                  if(!newbase)

                     exit(OVERFLOW);

                  L.elem = newbase;

                  L.listsize += LISTINCREMENT;  

                }

              q = &L.elem[i-1];

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

                 *(p+1) = *p;

                 *q = e;

                 L.length++;

                 return OK;

    }

   

void main()

{

 

    int i,n,m;

   

   

    SqList La;

    InitList(La);

    printf("/nInput the length of the list La: ");

    scanf("%d",&n);

    La.length = n;

    CreateList(La);

   

    SqList Lb;

    InitList(Lb);

    printf("/nInput the length of the list Lb: ");

    scanf("%d",&m);

    Lb.length = m;

    CreateList(Lb);

   

   

    SqList Lc;

    InitList(Lc);

   

    SqList Ld;

    InitList(Ld);

    Ld.length = m + n;

   

    int *pa,*pb,*pd;

    int *pa_last,*pb_last;

 

    pa = La.elem;

    pb = Lb.elem;

 

    Ld.listsize = Ld.length = La.length + Lb.length;

    pd = Ld.elem = (int *)malloc(Ld.listsize*sizeof(int));

    if(!Ld.elem)exit(OVERFLOW);

   

    pa_last =La.elem + La.length-1;

    pb_last =Lb.elem + Lb.length-1;

    while(pa <= pa_last  && pb <= pb_last)

    {

      

       if(*pa <= *pb)

       {

       *pd++ = *pa++;

       }

       else

       {

       *pd++ = *pa++;

       }

    }

      

    while( pa <= pa_last) *pd++ = *pa++;

    while( pb <= pb_last) *pd++ = *pb++;

   

 

    for(i = 1;i <= Ld.length;i++)

    {

       int *e;

       int j;

          

       e = &Ld.elem[i];

       j = LocateElem( Lc, *e,compare);

       if(!j)

          

           ListInsert(Lc, Lc.length+1,*e);

          

    }

   

      

   

printf("output the datas of La !/n");

OutputList(La);

printf("output the datas of Lb !/n");

    OutputList(Lb);

    printf("output the datas of Ld !/n");

    OutputList(Ld);

    printf("output the datas of Lc !/n");

    OutputList(Lc);

   

}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值