兩個順序線性表的合併,關於數組指針的操作。

線性表的順序存儲結構,重點在兩個線性表的合併。

對於數組可以使用指針也可以使用帶下標的數組來表示每個元素。

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章