修改了语法错误,至少可以编译通过#include
#include
#include
#define TRUE1
#define FALSE0
#define OK1
#define ERROR0
#define INFEASIBLE-1
#define OVERFLOW-2
#define list_init_size100
/* 线性表存储空间的初始分配量 */
#define LISTINCREMENT 10
/* 线性表存储空间的分配增量 */
typedef intStatuss;
typedef intElemType;
typedef struct {
ElemType *elem;
/* 存储空间基址 */
int length;
/* 当前长度 */
int listsize;
/* 当前分配的存储容量(以sizeof(ElemType)为单位) */
}SqList;
int InitList_Sq( SqList L )
{
/* 构造一个空的线性表L */
L.elem = (ElemType * ) malloc( list_init_size * sizeof(ElemType) );
if ( !L.elem )
exit( OVERFLOW );
/* 存储分配失败 */
L.length = 0;
/* 空表长度为0 */
L.listsize = list_init_size; /* 初始存储容量 */
return(OK);
};
/* Initlist_Sq */
int ListInsert_Sq( SqList L, int i, ElemType e )
{
/*
* 在顺序线性表L中第i个位置之前插入新的元素e,
* i的合法值为1<=i<=ListLength_Sq(L)+1
*/
ElemType *p, *q, *newbase;
/* 定义指针 */
if ( i L.length + 1 )
return(ERROR);
/* i值不合法 */
if ( L.length >= L.listsize )
{
/* 当前存储空间已满,增加分配 */
newbase = (ElemType * ) realloc( L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType) );
if ( i <= L.length )
return(i);
else return(0);
}
}
/* LocateElem_Sq */
void MergeList_Sq( SqList La, SqList Lb, SqList Lc )
{
/*
* 已知顺序线性表La和Lb的元素按值非递减排列
* 归并La和Lb得到新的顺序线性表Lc,Lc的元素也按非递减排列
*/
ElemType *pa, *pb, *pc, *pa_last, *pb_last;
pa= La.elem;
pb= Lb.elem;
Lc.listsize= Lc.length = La.length + Lb.length;
pc= Lc.elem = (ElemType *) malloc( Lc.listsize * sizeof(ElemType) );
if ( !Lc.elem )
exit( OVERFLOW ); /* 存储分配失败 */
pa_last = La.elem + La.length - 1;
pb_last = Lb.elem + Lb.length - 1;
while ( pa <= pa_last && pb <= pb_last )
{
/* 归并*/
if(*pa<=*pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while ( pa <= pa_last )
*pc++ = *pa++;
/* 插入La的剩余元素 */
while ( pb <= pb_last )
*pc++ = *pb++;
/* 插入Lb的剩余元素 */
}
/* MergeList_Sq */
void main()
{
SqList La, Lb, Lc;
InitList_Sq( La );
ListInsert_Sq( La, 1, 3 );
ListInsert_Sq( La, 2, 5 );
ListInsert_Sq( La, 3, 8 );
ListInsert_Sq( La, 4, 11 );
printf( "La插入后:\n" );
//display( La );
InitList_Sq( Lb );
ListInsert_Sq( Lb, 1, 2 );
ListInsert_Sq( Lb, 2, 6 );
ListInsert_Sq( Lb, 3, 8 );
ListInsert_Sq( Lb, 4, 9 );
ListInsert_Sq( Lb, 5, 11 );
ListInsert_Sq( Lb, 6, 15 );
ListInsert_Sq( Lb, 7, 20 );
printf( "Lb插入后:\n" );
//display( Lb );
MergeList_Sq( La, Lb, Lc );
printf( "归并后:\n" );
//display( Lc );
printf( "\n" );
//int a = LocateElem_Sq( Lc, 5 );
//printf( "%d\n", a );
}
温馨提示:答案为网友推荐,仅供参考