昨天看数据结构的合并,写了一个线性表的合并,出现不少问题,最后在别人帮助下解决了:
出现问题如下:第一是不小心进入了死循环、第二是地址改变了,没有改到原来的地址,导致打印错误。
直接上正确的代码吧:
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef int ElemType;
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量
int compareEqual(ElemType a,ElemType b){ //比较两个元素
if(a==b) return TRUE;
else
return FALSE;
}
typedef struct{
ElemType *elem; //指向数组的指针,存储空间基址
int length; //当前长度
int listsize; //当前分配的容量
}SqList;
Status InitList_Sq(SqList *L){ //构造一个空的线性表
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); //为线性表分配空间,elem为空间的起始地址
if(!L->elem)
return ERROR;
L->length = 0; //空表的当前长度为0
L->listsize = LIST_INIT_SIZE;
return TRUE;
}
Status ListInsert_Sq(SqList *L,int i,ElemType e) //在线性表第i个位置前插入一个元素e
{
ElemType *p,*q;
if(i<1 || i>L->length+1) //插入的位置不合适(不能为第0个位置和超过当前长度的2个位置)
return ERROR;
if(L->length >= L->listsize){ //线性表空间不够,重新分配空间
L->elem = (ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!L->elem)
return ERROR;
L->listsize += LISTINCREMENT; //修改线性表的长度
}
q = &(L->elem[i-1]); //q为插入位置
if(L->length > 0){ //这里主要是为了在空表的时候插入
for(p=&(L->elem[L->length-1]);p>=q;p--) //插入位置以及后面的元素后移一个长度
*(p+1) = *p;
}
*q = e;
L->length++; //修改长度
return TRUE;
}
Status ListDelete_Sq(SqList *L,int i,ElemType *e)
{
int j;
if(i<1 || i>L->length || L->length == 0)
return ERROR;
*e = L->elem[i-1];
for(j=i-1;j<L->length-1;j++)
L->elem[j] = L->elem[j+1]; //删除之后的元素前移
L->length--;
return TRUE;
}
void ListTraverse_Sq(SqList *L) //遍历线性表
{
int i;
for(i=0;i<L->length;i++)
printf("%d \n",L->elem[i]);
}
int LocateElem_Sq(SqList *L,ElemType e,int (*cmp)(ElemType el,ElemType e)){ //e在线性表中,则返回e的位置
int i=0;
for(i=0;i<L->length;i++){
if(cmp(L->elem[i],e))
return i;
}
return 0;
}
Status PriorElem_Sq(SqList *L,ElemType element,ElemType *e) //element在线性表中,则将element前驱赋给e
{
int loc;
loc = LocateElem_Sq(L,element,compareEqual);
if(loc > 0){
*e = L->elem[loc-1];
return TRUE;
}
else
return FALSE;
}
Status NextElem_Sq(SqList L,ElemType element,ElemType *e){//element在线性表中,则将element后继赋给e
int loc;
loc=LocateElem_Sq(&L,element,compareEqual);
if(loc<L.length-1){
*e=L.elem[loc+1];
return TRUE;
}
else
return FALSE;
}
/*int MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc)
{
ElemType *pa,*pb,*pc,*La_Last,*Lb_Last;
pa = La->elem;
pb = Lb->elem;
Lc->listsize = Lc->length = La->length + Lb->length;
pc = Lc->elem = (ElemType*)malloc(Lc->listsize*sizeof(ElemType));
Lc->elem = (ElemType*)realloc(Lc->elem,Lc->listsize*sizeof(ElemType));
if(!Lc->elem)
return ERROR;
La_Last = La->elem+La->length-1;
Lb_Last = Lb->elem + Lb->length-1;
while(pa <= La_Last && pb <= Lb_Last){
if(*pa <= *pb)
*pc++ = *pa++;
else
*pc++ = *pb++;
}
while(pa <= La_Last){
*pc++ = *pa++;
}
while(pb <= Lb_Last){
*pc++ = *pb++;
}
return 0;
}*/
int MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc)
{
int *La_Last,*Lb_Last;
Lc->listsize = Lc->length = La->length + Lb->length;
Lc->elem = (ElemType*)realloc(Lc->elem,Lc->listsize*sizeof(ElemType));
if(!Lc->elem)
return ERROR;
La_Last = La->elem+La->length-1;
Lb_Last = Lb->elem+Lb->length-1;
while(La->elem <= La_Last && Lb->elem <= Lb_Last){//La->elem <= La_Last = La->elem+La->length-1 这样是错误的,因为执行下面的语句就会进入死循环,导致段错误
if(*(La->elem) <= *(Lb->elem))
*Lc->elem++ = *La->elem++;
else
*Lc->elem++ = *Lb->elem++;
}
while(La->elem <= La_Last){
*Lc->elem++ = *La->elem++;
}
while(Lb->elem <= Lb_Last){
*Lc->elem++ = *Lb->elem++;
}
Lc->elem = Lc->elem - Lc->listsize; //这句一定要加上,因为Lc->elem的地址已经改变了,如果打印里面的数据只能从改变地址处开始打印
return 0;
}
Status ListLength_Sq(SqList *L) //线性表的长度
{
if(!L->elem)
return ERROR;
return L->length;
}
void DestoryList_Sq(SqList *L) //销毁线性表
{
if(L->elem)
free(L->elem);
L->length = 0;
}
int main()
{
SqList La,Lb,Lc;
// ElemType e;
InitList_Sq(&La);
InitList_Sq(&Lb);
InitList_Sq(&Lc);
ListInsert_Sq(&La,1,2);
ListInsert_Sq(&La,2,4);
ListInsert_Sq(&La,3,6);
ListInsert_Sq(&La,4,8);
printf("\nList Length is:%d\n",ListLength_Sq(&La));
ListTraverse_Sq(&La);
ListInsert_Sq(&Lb,1,3);
ListInsert_Sq(&Lb,2,5);
ListInsert_Sq(&Lb,3,7);
ListInsert_Sq(&Lb,4,9);
ListInsert_Sq(&Lb,5,11);
printf("\nList Length is:%d\n",ListLength_Sq(&Lb));
ListTraverse_Sq(&Lb);
MergeList_Sq(&La,&Lb,&Lc);
printf("\nList Length is:%d\n",ListLength_Sq(&Lc));
ListTraverse_Sq(&Lc);
return 0;
}