穆霓凰宫羽cp文:数据结构试验 高手帮忙 运行一下 merglist为啥不行?有高加分的

来源:百度文库 编辑:高校问答 时间:2024/04/19 19:38:01
Sqlist 文件
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *L){
L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
Status ListInsert_Sq(SqList *L, int i, ElemType e){
int j;
if( i<1 || i>L->length+1 ) return(ERROR);
if( L->length >= L->listsize) return(ERROR);
for(j = L->length-1; j>=i-1; --j) L->elem[j+1] = L->elem[j];
L->elem[i-1] = e;
++(L->length);
return OK;
}
Status ListDelete_Sq(SqList *L, int i, ElemType *e){
int j;
if( (i<1) || (i>L->length) ) return ERROR;
*e = L->elem[i-1];
for(j = i; j<=L->length-1; ++j) L->elem[j-1] = L->elem[j];
--(L->length);
return OK;
}
Status GetElem_Sq(SqList L, int i, ElemType *e){
int j;
if( (i<1) ||(i>L.length) ) return ERROR;
*e = L.elem[i-1];
return OK;
}
Status DestroyList_Sq (SqList *L) {
free(L);
return OK;
}

void MergeList_Sq(SqList La, SqList Lb, SqList *Lc){
int i,j,k;
InitList_Sq(Lc);

i=1;j=1;k=0;
while((i<=La.length)&&(j<=Lb.length))
{
if(La.elem[i]<=Lb.elem[j]){ListInsert_Sq(Lc,++k,La.elem[i]);++i;}
else{ListInsert_Sq(Lc,++k,Lb.elem[j]);++j;}
}
while(i<=La.length)
{
ListInsert_Sq(Lc,++k,La.elem[i]);
}
while(j<=Lb.length)
{
ListInsert_Sq(Lc,++k,Lb.elem[j]);
}

}
main 程序
#include <stdio.h>
#include <stdlib.h>
#include "predef.c"
typedef int ElemType;
#include "SqList.c"
void main( ){
int i, n,n1,n2;
ElemType e;
SqList L, La,Lb,Lc ;
InitList_Sq(&L);
InitList_Sq(&La);
InitList_Sq(&Lb);
InitList_Sq(&Lc);

printf("the number of Element?\t");
scanf("%d", &n);
printf("input your Elements:\n");
for(i=1; i<=n; ++i){
scanf("%d",&e); t5
ListInsert_Sq(&L, i, e);
}
printf("the result of GetElem operation:\n");
for(i=1; i<=n; ++i){
GetElem_Sq(L, i, &e);
printf("%d\t%d\n", e, L.length);
}
printf("the result of ListDelete operation:\n");
for(i=1; i<=n; ++i){
ListDelete_Sq(&L, 1, &e);
printf("%d\t%d\n", e, L.length);
}
printf("the number of Element of La?\t");
scanf("%d", &n1);
printf("input your Elements of La:\n");
for(i=1; i<=n1; ++i){
scanf("%d",&e);
ListInsert_Sq(&La, i, e);
}
printf("the number of Element of Lb?\t");
scanf("%d", &n2);
printf("input your Elements of Lb:\n");
for(i=1; i<=n2; ++i){
scanf("%d",&e);
ListInsert_Sq(&Lb, i, e);
}

printf("the result of Mergelist operation:\n");
MergeList_Sq( La, Lb, &Lc);
for(i=1;i<=n1+n2;i++)
printf("%d\n",Lc.elem[i]);

DestroyList_Sq(&Lc);

}
predefc文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;