动态数组出错,realloc调用后原数据被改变 10
按理说用realloc函数不会改变原数组的数据,但调用后结构体中一个数值竟然被改变了...代码片断如下:voidsequence(intexplength,List*SL...
按理说用realloc函数不会改变原数组的数据,但调用后结构体中一个数值竟然被改变了...代码片断如下:
void sequence(int explength, List *SList )
{
int i=0;
List * SInsert(List *,int, int,int);
int SDelete(List*, int,int);
void SDestroy(List *);
int Sprint(List*);//遍历后输出并得到实际长度
int insert=0;
int dele=0;
int locate=0;
int reallength=0;
List *head=NULL;
//建立顺序表
head=SList;
printf("input integers:\n");
for(i=0;i<explength;i++)
{
scanf("%d",&SList->a);
SList->listsize=i+1;//记录当前链表长度
SList++;
}
reallength=Sprint(head);
//插入操作
printf("insert e before No.i:\ne=");
scanf("%d",&insert);
printf("i=");
scanf("%d",&locate);
//检查参数
if(locate<1||locate>(explength+1))
{
printf("wrong location");
exit(0);
}
SList=SInsert(head,insert, locate,reallength);
printf("the result is\n");
reallength=Sprint(head);
printf("%d\n",reallength);
//删除操作
printf("delete No.i:\ni=");
scanf("%d",&locate);
if(locate<1||locate>explength)
{
printf("wrong location");
exit(0);
}
dele=SDelete(head, locate,reallength);
printf("%d\n",dele);
printf("the result is\n");
reallength=Sprint(head);
printf("%d\n",reallength);
SDestroy(head);
}
List* SInsert(List* list, int in, int loc,int length)
{
List *newbase=NULL;
List *locp=NULL,*locq=NULL;
int increase=1;
int temp;
int Sprint(List*);
//分配新空间
temp=Sprint(list);
newbase=(List*)realloc(list,sizeof(list)*(length+1));
printf("%d",sizeof(list)*(length+1));
temp=Sprint(newbase);
if(!newbase)
{exit(0);}
list=newbase;
//记录插入位置
locq=list+(loc-1);
for(locp=list+(length-1);locp>=locq;--locp)
{
locp->listsize=locp->listsize+1;
*(locp+1)=*(locp);
}
//插入数据并修改插入初链表长度
locq->a=in;
locq->listsize=loc;
return list;
}
int Sprint(List* list)
{
int i=0;
while(list->listsize>0 && list->listsize<=MAXLENGTH)//长度不为空时输出
{
printf("present length is %d,the integer is %d\n",list->listsize, list->a);
i++;
list++;
}
return i;
} 展开
void sequence(int explength, List *SList )
{
int i=0;
List * SInsert(List *,int, int,int);
int SDelete(List*, int,int);
void SDestroy(List *);
int Sprint(List*);//遍历后输出并得到实际长度
int insert=0;
int dele=0;
int locate=0;
int reallength=0;
List *head=NULL;
//建立顺序表
head=SList;
printf("input integers:\n");
for(i=0;i<explength;i++)
{
scanf("%d",&SList->a);
SList->listsize=i+1;//记录当前链表长度
SList++;
}
reallength=Sprint(head);
//插入操作
printf("insert e before No.i:\ne=");
scanf("%d",&insert);
printf("i=");
scanf("%d",&locate);
//检查参数
if(locate<1||locate>(explength+1))
{
printf("wrong location");
exit(0);
}
SList=SInsert(head,insert, locate,reallength);
printf("the result is\n");
reallength=Sprint(head);
printf("%d\n",reallength);
//删除操作
printf("delete No.i:\ni=");
scanf("%d",&locate);
if(locate<1||locate>explength)
{
printf("wrong location");
exit(0);
}
dele=SDelete(head, locate,reallength);
printf("%d\n",dele);
printf("the result is\n");
reallength=Sprint(head);
printf("%d\n",reallength);
SDestroy(head);
}
List* SInsert(List* list, int in, int loc,int length)
{
List *newbase=NULL;
List *locp=NULL,*locq=NULL;
int increase=1;
int temp;
int Sprint(List*);
//分配新空间
temp=Sprint(list);
newbase=(List*)realloc(list,sizeof(list)*(length+1));
printf("%d",sizeof(list)*(length+1));
temp=Sprint(newbase);
if(!newbase)
{exit(0);}
list=newbase;
//记录插入位置
locq=list+(loc-1);
for(locp=list+(length-1);locp>=locq;--locp)
{
locp->listsize=locp->listsize+1;
*(locp+1)=*(locp);
}
//插入数据并修改插入初链表长度
locq->a=in;
locq->listsize=loc;
return list;
}
int Sprint(List* list)
{
int i=0;
while(list->listsize>0 && list->listsize<=MAXLENGTH)//长度不为空时输出
{
printf("present length is %d,the integer is %d\n",list->listsize, list->a);
i++;
list++;
}
return i;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询