Sqlist &L只可用于c++中;其中&表示引用的意思,而在C中&只表示取址,因此C++中下列顺序表插入函数中的L实际就是原结构体
1.证明Sqlist &L就是k:
bool ListInsert(SeqList &L,int i,ElemType e){
if(i<1||i>L.length+1)
return false;
if(L.length>=L.MaxSize)
return false;
for (int j=L.length;j>=i;j--)
L.data[j]=L.data[j-1];
L.length++;
L.data[i]=e;
printf("%pn",&L);//对L取地址打印
return true;
}
证明,将ListInsert函数中的L进行取地址,将原结构体取址对比
int main(){
SeqList k;
int i=0,e=0;
k.data=(ElemType *)malloc(sizeof(ElemType)*InitSize);
k.MaxSize=20;
while(i!=15){
k.data[i]=i;
printf("%dt",k.data[i]);
i++;
}
k.length=i;
printf("%pn",&k); //原结构体地址
printf("please input the number and the position that you want to insert:n");
scanf("%d%d",&i,&e);
if((ListInsert(k,i,e))==true){
printf("it is succeed and the result is:n");
for (int j=0;j
运行结果
可见插入前 插入后和函数中的L地址都是同一个,得证&L就是结构体k;
2.证明Sqlist L与原结构体并非同一结构体但是具有回传功能。
对ListInsert 函数稍作修改
bool ListInsert(SeqList L,int i,ElemType e){
if(i<1||i>L.length+1)
return false;
if(L.length>=L.MaxSize)
return false;
for (int j=L.length;j>=i;j--)
L.data[j]=L.data[j-1];
L.length++;
L.data[i]=e;
printf("%pn",&L);//打印L地址
for (int j=0;j
运行结果
可见结构体K的地址插入前后并没有发生变化,而函数ListInsert的参数的结构体L地址却与k不同,证面Sqlist L是一个与原结构体相同的结构体,
如果采用该形式进行结构体修改,插入等操作时 ,当结构体数据量大时会增加电脑的额外开销。
结论: 记得点赞收藏



