#define overflow -1
#define ok 1
#define error 0
#define maxsize 16
#define increment 10
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int status;
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
} sqlist;
//初始化
status initlist(sqlist &L)
{
L.elem = (elemtype *)malloc(maxsize * sizeof(elemtype));
if (!L.elem)
exit(overflow);
L.length = 0;
L.listsize = maxsize;
return ok;
}
//创建
sqlist creata_list(sqlist L)
{
int i=0,N;
printf("input length you want -->\n");
scanf("%d",&N);
while( i!= N )
{
scanf("%d",&L.elem[i]);
i++;
}
L.length += N;
return L;
}
//在指定位置插入数字
status insertlist(sqlist &L, int i, elemtype e)
{
if (i < 1 || i > L.length)
return error;
elemtype *p;
int j;
if (L.length >= L.listsize)
{
p = (elemtype *)realloc(L.elem, (L.listsize + increment) * sizeof(elemtype));
if (!p)
exit(overflow);
L.elem = p;
L.listsize += increment;
}
for (j = L.length - 1; j >= i - 1; --j)
L.elem[j + 1] = L.elem[j];
L.elem[j + 1] = e;
L.length++;
return ok;
}
//输出顺序表
void printlist(sqlist L)
{
int i;
for (i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
}
//删除相应位置数据
status dellist(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; j++)
L.elem[j - 1] = L.elem[j];
--L.length;
return ok;
}
//获取相应位置数据
int LocatedList(sqlist &L,int j)
{
if(j<1 || j>L.length){
printf("The specified location does not exist!");
return error;
}
return L.elem[j-1] ; //直接返回
}
//获取相关值的位置
int GetList(sqlist L,elemtype temp)
{
int j;
for( j=0 ; j<L.length ; j++){
if(L.elem[j]==temp) {
return j;
break;
}
}
printf("dont exist!\n");
return error;
}
//合并集合
sqlist MergeList(sqlist La,sqlist Lb,sqlist *Lc)
{
Lc->length=La.length+Lb.length;
Lc->listsize=Lc->length;
elemtype *pa=La.elem,*pb=Lb.elem,*pc;
pc=(elemtype*)malloc(Lc->length*sizeof(elemtype)); //申请新的内存
Lc->elem=pc;
elemtype *pa_last,*pb_last; //定义指向初地址和末地址的指针
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 if(*pa > *pb) *pc++ = *pb++;
else if(*pa = *pb) { *pc++ = *pa++; *pb++; }; //顺序存储,重复只存pa,pa,pb都后退
}
while(pa <= pa_last) *pc++ = *pa++;
while(pb <= pb_last) *pc++ = *pb++; //判空,不为空继续赋值,直到指向最后指针。
return *Lc;
}
//合并顺序表
sqlist CombineList(sqlist L1,sqlist L2,sqlist *L3)
{
L3->length=L1.length+L2.length;
L3->listsize=L3->length;
elemtype *p1=L1.elem,*p2=L2.elem,*p3;
p3=(elemtype*)malloc(L3->length*sizeof(elemtype)); //申请新的内存
L3->elem=p3;
elemtype *p1_last,*p2_last; //定义指向初地址和末地址的指针
p1_last=L1.elem+L1.length-1;
p2_last=L2.elem+L2.length-1;
while (p1 <= p1_last && p2 <= p2_last) {
if( *p1<=*p2 ) *p3++ = *p1++;
else *p3++ = *p2++;
}
while(p1 <= p1_last) *p3++ = *p1++;
while(p2 <= p2_last) *p3++ = *p2++; //判空,不为空继续赋值,直到指向最后指针。
return *L3;
}
int main()
{
sqlist LL;
elemtype x,data;
int r, i,j;
if (!initlist(LL)) //初始化
return error;
printf("input data-->\n"); //创建
LL=creata_list(LL);
printf("check Order table-->\n"); //显示
printlist(LL);
printf("value index-->\n"); //插入
scanf("%d %d", &x, &r);
insertlist(LL, r, x);
printlist(LL);
printf("index-->\n"); //删除
scanf("%d", &r);
dellist(LL,r,x);
printlist(LL);
printf("input locate-->\n");
scanf("%d",&j);
data=LocatedList(LL,j);
printf("the elem[%d] = %d\n",j,data);
printf("input value-->\n");
elemtype temp,place;
scanf("%d",&temp);
place=GetList(LL, temp);
printf("%d = the elem[%d]\n",temp,place+1);
sqlist La,Lb,Lc; //合并集合
printf("Create list La-->\n");
initlist(La);
La=creata_list(La);
printf("check La\n");
printlist(La);
printf("Create list Lb-->\n");
initlist(Lb);
Lb=creata_list(Lb);
printf("check Lb\n");
printlist(Lb);
printf("combine list-->\n");
MergeList(La,Lb,&Lc);
printlist(Lc);
sqlist L1,L2,L3; //合并有序表
printf("Create list L1-->\n");
initlist(L1);
L1=creata_list(L1);
printf("check L1\n");
printlist(L1);
printf("Create list L2-->\n");
initlist(L2);
L2=creata_list(L2);
printf("check L2\n");
printlist(L2);
printf("combine list-->\n");
MergeList(L1,L2,&L3);
printlist(L3);
}
原代码第132行,if()里少了一个 ‘= ’号,其他处修改如下,供参考:
#define overflow -1
#define ok 1
#define error 0
#define maxsize 16
#define increment 10
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int status;
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
} sqlist;
//初始化
status initlist(sqlist &L)
{
L.elem = (elemtype *)malloc(maxsize * sizeof(elemtype));
if (!L.elem)
exit(overflow);
L.length = 0;
L.listsize = maxsize;
return ok;
}
//创建
sqlist creata_list(sqlist L)
{
int i=0,N;
printf("input length you want -->\n");
scanf("%d",&N);
while( i!= N )
{
scanf("%d",&L.elem[i]);
i++;
}
L.length += N;
return L;
}
//在指定位置插入数字
status insertlist(sqlist &L, int i, elemtype e)
{
if (i < 1 || i > L.length)
return error;
elemtype *p;
int j;
if (L.length >= L.listsize)
{
p = (elemtype *)realloc(L.elem, (L.listsize + increment) * sizeof(elemtype));
if (!p)
exit(overflow);
L.elem = p;
L.listsize += increment;
}
for (j = L.length - 1; j >= i - 1; --j)
L.elem[j + 1] = L.elem[j];
L.elem[j + 1] = e;
L.length++;
return ok;
}
//输出顺序表
void printlist(sqlist L)
{
int i;
for (i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
}
//删除相应位置数据
status dellist(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; j++)
L.elem[j - 1] = L.elem[j];
--L.length;
return ok;
}
//获取相应位置数据
int LocatedList(sqlist &L,int j)
{
if(j<1 || j>L.length){
printf("The specified location does not exist!");
return error;
}
return L.elem[j-1] ; //直接返回
}
//获取相关值的位置
int GetList(sqlist L,elemtype temp)
{
int j;
for( j=0 ; j<L.length ; j++){
if(L.elem[j]==temp) {
return j;
break;
}
}
printf("dont exist!\n");
return error;
}
//合并集合
sqlist MergeList(sqlist La,sqlist Lb,sqlist *Lc)
{
int cnt=0; //修改
Lc->length=La.length+Lb.length;
//Lc->listsize=Lc->length; //修改
elemtype *pa=La.elem,*pb=Lb.elem,*pc;
pc=(elemtype*)malloc(Lc->length*sizeof(elemtype)); //申请新的内存
Lc->elem=pc;
elemtype *pa_last,*pb_last; //定义指向初地址和末地址的指针
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 if(*pa > *pb) *pc++ = *pb++;
else if(*pa == *pb) { *pc++ = *pa++; *pb++;cnt++;}//顺序存储,重复只存pa,pa,pb都后退
} //修改 =
while(pa <= pa_last) *pc++ = *pa++;
while(pb <= pb_last) *pc++ = *pb++; //判空,不为空继续赋值,直到指向最后指针。
while(cnt--) Lc->length--; //修改
Lc->listsize=Lc->length;
return *Lc;
}
//合并顺序表
sqlist CombineList(sqlist L1,sqlist L2,sqlist *L3)
{
L3->length=L1.length+L2.length;
L3->listsize=L3->length;
elemtype *p1=L1.elem,*p2=L2.elem,*p3;
p3=(elemtype*)malloc(L3->length*sizeof(elemtype)); //申请新的内存
L3->elem=p3;
elemtype *p1_last,*p2_last; //定义指向初地址和末地址的指针
p1_last=L1.elem+L1.length-1;
p2_last=L2.elem+L2.length-1;
while (p1 <= p1_last && p2 <= p2_last) {
if( *p1<=*p2 ) *p3++ = *p1++;
else *p3++ = *p2++;
}
while(p1 <= p1_last) *p3++ = *p1++;
while(p2 <= p2_last) *p3++ = *p2++;//判空,不为空继续赋值,直到指向最后指针。
return *L3;
}
int main()
{
sqlist LL;
elemtype x,data;
int r, i,j;
if (!initlist(LL)) //初始化
return error;
printf("input data-->\n"); //创建
LL=creata_list(LL);
printf("check Order table-->\n"); //显示
printlist(LL);
printf("value index-->\n"); //插入
scanf("%d %d", &x, &r);
insertlist(LL, r, x);
printlist(LL);
printf("index-->\n"); //删除
scanf("%d", &r);
dellist(LL,r,x);
printlist(LL);
printf("input locate-->\n");
scanf("%d",&j);
data=LocatedList(LL,j);
printf("the elem[%d] = %d\n",j,data);
printf("input value-->\n");
elemtype temp,place;
scanf("%d",&temp);
place=GetList(LL, temp);
printf("%d = the elem[%d]\n",temp,place+1);
sqlist La,Lb,Lc; //合并集合
printf("Create list La-->\n");
initlist(La);
La=creata_list(La);
printf("check La\n");
printlist(La);
printf("Create list Lb-->\n");
initlist(Lb);
Lb=creata_list(Lb);
printf("check Lb\n");
printlist(Lb);
printf("combine list-->\n");
//MergeList(La,Lb,&Lc);
CombineList(La,Lb,&Lc); //修改
printlist(Lc);
sqlist L1,L2,L3; //合并有序表
printf("Create list L1-->\n");
initlist(L1);
L1=creata_list(L1);
printf("check L1\n");
printlist(L1);
printf("Create list L2-->\n");
initlist(L2);
L2=creata_list(L2);
printf("check L2\n");
printlist(L2);
printf("MergeList list-->\n");
MergeList(L1,L2,&L3);
printlist(L3);
return 0;
}