主题:顺序表归并,急救!
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define Listsize 10
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef struct
{
int *elem;
int listsize;
int length;
}SqList;
Status InitSqList(SqList *L)
{
L=(SqList *)malloc ((*L).listsize * sizeof(int));
if(!(*L).elem) exit(OVERFLOW);
(*L).length=0;
return OK;
}
int ListLength(SqList L)
{
return(L.length);
}
Status Getelem(SqList L,int i,int *e)
{
int *p;
if(L.length==0) return OVERFLOW;
if(i<1||i>ListLength(L)) return OVERFLOW;
p=L.elem+1;
int h=0;
while(p&&h<i)
{
h++;
p=++p;
}
e=p;
}
int ListInsert(SqList *L,int i,int e)
{
int *newbase,q,LISTINCREMENT,*p;
if(i<1||i>(*L).length+1)
return OVERFLOW;
if((*L).length>=(*L).listsize)
{
newbase=(int *)realloc((*L).elem,((*L).listsize+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=e;
++(*L).length;
return ok;
}
void MergeList(List La,List Lb,List &Lc)
{
InitList(Lc);
i=j=1;
k=0;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
while((i<=La_len)&&(j<=Lb_len))
{
GetElem(La,i,ai);
GetElem(Lb,j,bj);
if(ai<=bj)
{
ListInsert(Lc,++k,ai);++i;
}
else
{
ListInsert(Lc,++k,bj);++j;
}
}
while(i<=La_len)
{
GetElem(La,i++,ai);
ListInsert(Lc,++k,ai);
}
while(j<=Lb_len)
{
GetElem(Lb,j++,bj);
ListInsert(Lc,++k,bj);
}
}
void main()
{
int La={1,2,3,4};
int Lb={5,6,7,8};
int Lc;
MergeList(La,Lb,Lc);
}
久编后,仍有些小错,希望高手们改正一下!
#include<malloc.h>
#include<stdlib.h>
#define Listsize 10
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef struct
{
int *elem;
int listsize;
int length;
}SqList;
Status InitSqList(SqList *L)
{
L=(SqList *)malloc ((*L).listsize * sizeof(int));
if(!(*L).elem) exit(OVERFLOW);
(*L).length=0;
return OK;
}
int ListLength(SqList L)
{
return(L.length);
}
Status Getelem(SqList L,int i,int *e)
{
int *p;
if(L.length==0) return OVERFLOW;
if(i<1||i>ListLength(L)) return OVERFLOW;
p=L.elem+1;
int h=0;
while(p&&h<i)
{
h++;
p=++p;
}
e=p;
}
int ListInsert(SqList *L,int i,int e)
{
int *newbase,q,LISTINCREMENT,*p;
if(i<1||i>(*L).length+1)
return OVERFLOW;
if((*L).length>=(*L).listsize)
{
newbase=(int *)realloc((*L).elem,((*L).listsize+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=e;
++(*L).length;
return ok;
}
void MergeList(List La,List Lb,List &Lc)
{
InitList(Lc);
i=j=1;
k=0;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
while((i<=La_len)&&(j<=Lb_len))
{
GetElem(La,i,ai);
GetElem(Lb,j,bj);
if(ai<=bj)
{
ListInsert(Lc,++k,ai);++i;
}
else
{
ListInsert(Lc,++k,bj);++j;
}
}
while(i<=La_len)
{
GetElem(La,i++,ai);
ListInsert(Lc,++k,ai);
}
while(j<=Lb_len)
{
GetElem(Lb,j++,bj);
ListInsert(Lc,++k,bj);
}
}
void main()
{
int La={1,2,3,4};
int Lb={5,6,7,8};
int Lc;
MergeList(La,Lb,Lc);
}
久编后,仍有些小错,希望高手们改正一下!