您的位置:

c语言线性合并,两个线性表合并c语言

本文目录一览:

数据结构C语言版,顺序线性表的合并程序。最好有注释

//希望我的回答对你的学习有帮助

#include stdlib.h

/*顺序表存储空间长度的最小值*/

#define LISTMINSIZE 10

/*顺序表存储结构类型定义*/

typedef struct

{

               ListDT*base;       /*顺序表空间基地址*/

               intlistsize;            /*顺序表空间尺寸*/

               intlen;                  /*顺序表长度*/

}SeqList;

 

/*顺序表初始化*/

void ListInitialize(SeqList *pL, int size)

{

               if(sizeLISTMINSIZE)

                               size=LISTMINSIZE;           /*限定不能小于最小尺寸*/

               pL-listsize=size;

               pL-base=(ListDT*)malloc(pL-listsize*sizeof(ListDT));

               if(!pL-base)

                               exit(EXIT_FAILURE);

               pL-len=0;                                                        /*初始化空表*/

}

 

/*按给定的下标取顺序表元素值*/

BOOL ListElem(SeqList L, int index, ListDT *pelem)

{

               BOOLflg=TRUE;

               if(index0|| indexL.len-1 )

                               flg=FALSE;                                        /*参数越界*/

               else

                               *pelem=L.base[index];

               returnflg;

}

 

/*求顺序表长度*/

int ListLen(SeqList L)

{

               returnL.len; 

}

 

/*在顺序表中指定序号位置插入元素*/

BOOL ListInsert(SeqList *pL, int pos, ListDT d)

{

               BOOLflg=TRUE;

               inti;

               if(pos0|| pL-len=pL-listsize || pospL-len)

                               flg=FALSE;

               else

               {

                               for(i=pL-len-1;i=pos; i--)                             /*移动数据*/

                                              pL-base[i+1]=pL-base[i];

                               pL-base[pos]=d;                                              /*写入数据*/

                               pL-len++;                                                         /*表长增1*/

               }

               returnflg;

}

 

/*把顺序表中指定序号的元素删除*/

BOOL ListDel(SeqList *pL, int pos)

{

               BOOLflg=TRUE;

               inti;

               if(pos0|| pos=pL-len)

                               flg=FALSE;

               else

               {

                               for(i=pos+1;ipL-len; i++)                            /*移动数据*/

                                              pL-base[i-1]=pL-base[i];

                               pL-len--;                                                           /*表长增1*/

               }

               returnflg;

}

 

/*在顺序表中查找元素*/

int ListLoc(SeqList L, ListDT d,BOOL (*equal)(ListDT,ListDT))

{

               intpos=L.len-1;

               while(pos=0 !(*equal)(L.base[pos],d))

                               pos--;

               returnpos;

}

 

/*取前导元素序号位置*/

BOOL ListPrior(SeqList L, int pos, int *ppriorpos)

{

               BOOLflg=TRUE;

               if(pos0 posL.len)

                               *ppriorpos=pos-1;

               else

                               flg=FALSE;

               returnflg;

}

 

/*取后继元素序号位置*/

BOOL ListNext(SeqList L, int pos, int *pnextpos)

{

               BOOLflg=TRUE;

               if(pos=0 posL.len-1)

                               *pnextpos=pos+1;

               else

                               flg=FALSE;

               returnflg;

}

 

/*销毁顺序表*/

void ListDestroy(SeqList L)

{

               free(L.base);

}

 

#endif

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

 

/*

建议性测试用程序

*/

typedef enum {TRUE=1,FALSE=0} BOOL;

typedef int ListDT;

#include "seqlist.c"

void printSeqList(SeqList L)

{

               inti;

               ListDTx;

               printf("\nList:\n");

               for(i=0;iListLen(L); i++)

               {

                               ListElem(L,i,x);

                               printf("%3d",x);

               }

}

 

BOOL dataequal(int x, int y)

{

               return(x==y)? TRUE:FALSE;

}

 

#define N 5

void main()

{

               inti,prior,next;

               ListDTx,test[N]={10,20,30,40,50};

               SeqListL;

               /*初始化顺序表*/

               ListInitialize(L,N);

               /*在表头插入N个元素*/

               for(i=0;iN; i++)

                               ListInsert(L,0,test[i]);

               printSeqList(L);

               /*删除元素*/

               ListDel(L,N/2);

               printSeqList(L);

               printf("\ninputa key:\n");

               scanf("%d",x);

               /*查找x在表中位置*/

               i=ListLoc(L,x,dataequal);

               /*求x的前导元素*/

               if(ListPrior(L,i,prior))

               {

                               ListElem(L,prior,x);

                               printf("Prior:%d\n",x);

               }

               else

                               printf("noPrior.\n");

               /*求x的后继*/

               if(ListNext(L,i,next))

               {

                               ListElem(L,next,x);

                               printf("Next:%d\n",x);

               }

               else

                               printf("noNext.\n");

               /*求表长*/

               printf("Listlength=%d",ListLen(L));

               /*销毁顺序表*/

               ListDestroy(L);

}

c语言有序线性表合并数据

由于结果保存的表类型不同,

算法也有差别,

我选择结果保存为动态数组的算法,

其它算法可以自己搞.

1,

计算La的元素个数赋值给numOfLa;

计算Lb的元素个数赋值给numOfLb;

2,

分配能够容纳numOfLa+numOfLb个元素的内存空间,

首地址指针为pRes;

3,

设置变量counterLa并赋初值0,

设置变量counterLb并赋初值0,

设置变量counterRes并赋初值0

4,

当counterLa

numOfLa

并且

counterLb

numOfLb时做以下步:

4.1

如果La[counterLa]

等于Lb[counterLb]时,

pRes[couterRes]赋值为La[couterLa],

couterLa和counterLb

和counterRes三者均加一,

否则

4.2

如果La[counterLa]

小于Lb[counterLb]时,

pRes[couterRes]赋值为La[couterLa],

counterLa和couterRes两者均加一,

否则

4.3

pRes[couterRes]赋值为Lb[couterLb],

counterLb和couterRes两者均加一

5,

如果counterLa等于numOfLa,

则从i

=

couterLb

至i

numOfLb做以下:

5.1

pRes[counterRes]

赋值为

Lb[i]

5.2

counterRes和i两者增一;

6,

否则,

从i

=

couterLa

至i

numOfLa做以下:

6.1

pRes[counterRes]

赋值为

La[i]

6.2

counterRes和i两者增一;

7结束,

pRes中保存合并的线性表数据,

数据个数为counterRes;

在此基础上,

也可以写出链表的算法

如何用C语言把两个有序线性列表合并成一个有序线性链表(不超过10行的程序)

#includestdio.h

#include malloc.h

#includelimits.h

#define MAX 1024

void merge(int a[],int p,int q,int b[],int r,int s,int c[])

{int i,j,n1,n2,n,k,m;

n1=q-p+1;

n2=s-r+1;

int *l=(int *)malloc(sizeof(int)*(n1+1));

for(i=0;in1;i++)

l[i]=a[p+i-1];

int *t=(int *)malloc(sizeof(int)*(n2+1)); for(j=0;jn2;j++)

t[j]=b[r+j-1];

l[n1]=INT_MAX;t[n2]=INT_MAX;

i=0;

j=0;

for(k=0;kn1+n2;k++)

{if(l[i]t[j]){c[k]=l[i];

i++;}

else {c[k]=t[j];

j++;}}

for(i=0;in1+n2;i++)

printf("%d ",c[i]);

printf("\n");

}

int main()

{

int i;

int arr[] = {4,5,9,1000,1001};

int arr2[] = {1,7,8,999,1003,1005};

int *res = (int*)malloc(sizeof(int) * 100);

merge(arr,1,5,arr2,1,6,res);

return 0;

}

求C语言程序代码,合并线性表

#includestdio.h

#includestdlib.h

#define MAXSIZE 110

typedef struct 

{

    int elem[MAXSIZE];

    int len;

}sqlist;

void merge(sqlist *la,sqlist *lb,sqlist *lc)

{

    int i,j,k;

    i=j=k=0;

    if(la-len+lb-len=MAXSIZE)

    {    

        while(ila-lenjlb-len)

        {

            if(la-elem[i]lb-elem[j])

            {

                lc-elem[k]=la-elem[i];

                i++;

                k++;

            }

                else

                {

                    lc-elem[k]=lb-elem[j];

                    j++;

                    k++;

                }

        }

        while(ila-len)

        {

            lc-elem[k]=la-elem[i];

            i++;

            k++;

        }

        while(jlb-len)

        {

            lc-elem[k]=lb-elem[j];

            j++;

            k++;

        }  

        lc-len=la-len+lb-len;

   }

       else printf("out of bound");

}

void print(sqlist *l)

{

    int i;

    for(i=0;il-len;i++)

    {

        printf("%d ",l-elem[i]);             

    }

printf("\n");

}

int main(void)

{

    int i;

    sqlist la;

    sqlist lb;

    sqlist lc;

printf("输入线性表LA的元素个数:");

scanf("%d",la.len);

printf("输入线性表LA的%d个元素:",la.len);

for(i=0;ila.len;i++)

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

printf("输入线性表LB的元素个数:");

scanf("%d",lb.len);

printf("输入线性表LB的%d个元素:",lb.len);

for(i=0;ilb.len;i++)

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

    merge(la,lb,lc);

    print(lc);

    system("pause");

    return(0);

}