正在学习数据结构,练习双链表,请教一下大家,谢谢~
//成员函数
int ListDelete(DuLinklist &L,int i){//删除第i个结点
DuLinklist p,q;p,q=L->next;
if(!(p=LocateElem(L,i))) return ERROR;
p->prior->next=p->next;
p->next->prior=p->prior;
delete p;
cout<<q->data.name<<endl;//这里为了检测首元结点数据是否正常
return OK;
}
//主函数
int main(){
DuLinklist L,p;
student newvalue;//这是定义的结构体变量
int num[6];
char charname[5][2];
int a;
stu mystu(L);
cout<<"初始化成功"<<'\n'<<'\n';
for(int i=0;i<5;i++){//键入数据
cin>>num[i];
}
for(int i=0;i<5;i++)
for(int j=0;j<2;j++){
cin>>charname[i][j];
}
for(int i=4;i>=0;i--){
for(int j=0;j<2;j++){
string s(1,charname[i][j]);
newvalue.name=newvalue.name+s;
}
newvalue.num=num[i];
mystu.ListInsert(L,1,newvalue);
newvalue.name="";
}
cout<<"赋值成功"<<'\n'<<'\n';
cout<<"请输入需要删除的结点位置(1~5)"<<endl;
cin>>a;
mystu.ListDelete(L,a);
cout<<"删除成功"<<'\n'<<'\n';
p=L->next;cout<<p->data.name<<endl;//从这里发现头结点的next出现错误
mystu.display(L);
cout<<"显示成功"<<'\n'<<'\n';
return 0;
}
在成员函数中头结点之后的数据正常(aa)
在主函数中变成了dd
是不是你的LocaleElem函数修改了L的指向呢
你L头结点指针在LocateElem()函数中被修改了吧
你发下LocateElem()函数的代码看看