64,282
社区成员
发帖
与我相关
我的任务
分享
int InsertList_Sq(Sq_List &L, int i, ElemType e)
{
ElemType *p, *q;
ElemType *newbase;
if (i < 1 || i > L.length + 1)
return 1;
if (L.length >= L.listsize)
{
newbase = (ElemType *)realloc(L.elem, sizeof(ElemType)* (LIST_INCREMENT + LIST_INIT_SIZE));
if (!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize = LIST_INIT_SIZE + LIST_INCREMENT;
}
p = &L.elem[i - 1];
for (q = &L.elem[L.length - 1]; q >= p; --q) //顺序表中所有元素向后移动一个位置
{
*(q + 1) = *q;
}
*p = e; //debug到这里的时候,显示p指针指向的数据是正确的,但是L数组中就是没有输入啊,p指针分明是指向了数组中的一个元素啊。。。。。。
L.length++; //表长加1
return 0;
}
int InsertList_Sq(Sq_List &L, int i, ElemType e)
{
ElemType *p, *q;
ElemType *newbase;
int newsize;
if (i < 1 || i > L.length + 1)
return 1;
if (L.length >= L.listsize)
{
newsize=L.listsize;
while (1) {
newsize+=LIST_INCREMENT;
if (newsize>L.length) break;
}
newbase = (ElemType *)realloc(L.elem, sizeof(ElemType)* newsize);
if (!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize = newsize;
}
p = &L.elem[i - 1];
for (q = &L.elem[L.length - 1]; q >= p; --q) //顺序表中所有元素向后移动一个位置
{
*(q + 1) = *q;
}
*p = e;
L.length++; //表长加1
return 0;
}