两个顺序线性表的合并,关于数组指针的操作。

来源:互联网 发布:网站seo公司哪家专业 编辑:程序博客网 时间:2024/05/30 23:07

线性表的顺序存储结构,重点在两个线性表的合并。

对于数组可以使用指针也可以使用带下标的数组来表示每个元素。

#include <stdio.h>#include <conio.h>#include <stdlib.h>#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define OVERFLOW -1#define OK 1#define ERROR 0typedef int Elemtype;typedef int Status;typedef struct {    Elemtype *elem;    int length;    int listsize;}SqList;//线性表的动态分配顺序存储结构Status InitList(SqList *L){    L->elem=(Elemtype *)malloc(sizeof(LIST_INIT_SIZE));    if (!L->elem)        exit(OVERFLOW);    L->length=0;    L->listsize=LIST_INIT_SIZE;    return OK;}Status ListInsert(SqList *L,int i,Elemtype e){    Elemtype *newbase,*p,*q;    if (i<1||i>L->length+1)        return ERROR;    if (L->length>=L->listsize)    {        newbase=(Elemtype *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(Elemtype));        if (!newbase)            exit(OVERFLOW);        L->elem=newbase;        L->listsize+=LISTINCREMENT;    }    q=&(L->elem[i-1]);//i的前一个位置,使用的是数组    for(p=&(L->elem[L->length-1]);p>=q;--p)//元素右移        *(p+1)=*p;    *q=e;    ++L->length;    return OK;}Status ListDelete(SqList *L,int i,Elemtype &e)//引用{    Elemtype *p,*q;    if (i<1||i>L->length)        return ERROR;    p=&(L->elem[i-1]);    e=*p;    q=L->elem+L->length-1;    printf("p的第一个值%d\n",*p);    for (++p;p<=q;++p)    {        printf("p的值%d\n",*p);        *(p-1)=*p;    }    --L->length;    return OK;}//使用指针void MergeList(SqList La,SqList Lb,SqList &Lc){    Elemtype *pc,*pa,*pb,*palast,*pblast;    int ppc,ppa,ppb,ppalast,ppblast;    pa=La.elem;    pb=Lb.elem;    ppa=0;    ppb=0;    ppalast=La.length-1;    ppblast=Lb.length-1;    Lc.listsize=Lc.length=La.length+Lb.length;    pc=Lc.elem=(Elemtype*)malloc(Lc.listsize*sizeof(Lc.listsize*sizeof(Elemtype)));    if (!Lc.elem)        exit(OVERFLOW);    palast=La.elem+La.length-1;    pblast=Lb.elem+Lb.length-1;    while(pa<=palast&&pb<=pblast)    {//归并        printf("%d %d",La.elem[pa],Lb.elem[pb]);        if (*pa<=*pb)            *pc++=*pa++;        else            *pc++=*pb++;    }    while(pa<=palast)        *pc++=*pa++;    while(pb<=pblast)        *pc++=*pb++;}//不用指针,用数组实现/*void MergeList(SqList La,SqList Lb,SqList &Lc){    int ppc=0,ppa,ppb,ppalast,ppblast;    ppa=0;    ppb=0;    ppalast=La.length-1;    ppblast=Lb.length-1;    Lc.listsize=Lc.length=La.length+Lb.length;    Lc.elem=(Elemtype*)malloc(Lc.listsize*sizeof(Lc.listsize*sizeof(Elemtype)));    if (!Lc.elem)        exit(OVERFLOW);    while(ppa<=ppalast&&ppb<=ppblast)    {        if (La.elem[ppa]>=Lb.elem[ppb])        {            Lc.elem[ppc]=Lb.elem[ppb];            ppc++;            ppb++;        }        else        {            Lc.elem[ppc]=La.elem[ppa];            ppc++;            ppa++;        }    }    while(ppa<=ppalast)    {        Lc.elem[ppc]=La.elem[ppa];        ppc++;        ppa++;    }    while(ppb<=ppblast)    {        Lc.elem[ppc]=Lb.elem[ppb];        ppc++;        ppb++;    }}*/void main(){    int elem,e,i=1,j;    SqList L;    printf("初始化\n");    InitList(&L);    printf("L:\n请输入元素:\n");    scanf("%d",&elem);    while(elem!=0)    {        ListInsert(&L,i,elem);        printf("请输入元素\n");        scanf("%d",&elem);        i++;    }    printf("输出元素:\n");    for (j=0;j<L.length;j++)    {        printf("%d ",L.elem[j]);    }    printf("删除元素:\n");    scanf("%d",&i);    ListDelete(&L,i,e);    printf("输出元素:\n");    for (j=0;j<L.length;j++)    {        printf("%d ",L.elem[j]);    }    SqList L1;    InitList(&L1);    i=1;    printf("L1:\n请输入元素:\n");    scanf("%d",&elem);    while(elem!=0)    {        ListInsert(&L1,i,elem);        printf("请输入元素\n");        scanf("%d",&elem);        i++;    }    printf("\nL1输出元素:\n");    for (j=0;j<L1.length;j++)    {        printf("%d ",L1.elem[j]);    }    SqList L2;    InitList(&L2);    MergeList(L,L1,L2);    printf("L2输出元素:\n");    for (j=0;j<L2.length;j++)    {        printf("%d ",L2.elem[j]);    }    getch();}

 

 

原创粉丝点击