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

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

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

#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 0
typedef 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();
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值