线性表怎么拷贝啊
void CopyVertexList1(const VertexList source,VertexList & destination)
{
for(long i=0; i < source.length ;i++)
{
ElemType data;
data = source.elem[i];
InsertLinearList(destination,data);
}
}
void InsertLinearList(VertexList & L,ElemType data)
{
//expand the VL arrary , if necessary
if(L.length > = L.listsize )
{
ElemType * newbase = (ElemType *) realloc(L.elem,(L.length +LIST_INCREMENT) * sizeof(ElemType));
if(!newvert)
exit(OVERFLOW);
L.vert = newvert;
L.listsize += LIST_INCREMENT;
}
// add the vertex to the end of the array
L.elem[L.length ++] = data;
return;
}
void CreateLinearList(VertexList & L)
{
L.elem = (ElemType *)malloc(LIST_SIZE * sizeof(ElemType));
L.length = 0;
L.listsize = LIST_SIZE;
return;
}
typedef struct
{
ElemType * elem;
long length;
long listsize;
}VertexList;
typedef struct ElemType
{
Vertex vertex; //顶点坐标
Vertex Normal; //法向量
A * a;
}ElemType;
typedef struct A
{
int position;
}A;
void DestructLinearList(VertexList & L)
{
if(L.elem )
free(L.elem);
L.vert = NULL;
return;
}
我是这样调用的:
VertexList VL;
CreateLineaList(VL);
CopyVertexList(VL,vl);
vl是已经存在的。
我想问的是在我调用
DestructLinearList(vl)的时候,VL的a也没有值了。不过它的vertex和Normal还在。我想问的是,这个带有指针的怎么拷贝啊?a指向了内存中的随机值啊。
那位高人知道,麻烦帮忙一下,谢谢!
如果position又是一指针怎么办啊?
拜托了啊。
[解决办法]
带有指针的,当然要拷贝指针指向的内容。
[解决办法]
那你的程序结构要修改了,要不也可以用指针了,然后把要用到那块数据给拷贝出来就行了.