C++作业,分享
[此贴子已经被作者于2006-7-1 17:31:11编辑过]
C++作业,分享
[此贴子已经被作者于2006-7-1 17:31:11编辑过]
刚才改了下:
void Delete(T &inform) //删除
{
bool found=false;CNode<T>* oldNode=NULL;
if(head==NULL) //空表
{cout<<"Can't delete from a empty list!\n";return;}
else if(head->next==NULL)//只有一个元素的表
{
if(head->info == inform)
{
delete head;delete tail;head=NULL;tail=NULL;found=true;
}
else found=false;
}
else
for(CNode<T>*newNode=oldNode=head;newNode;)
{
if(newNode->info==inform)
{
if(newNode==head)//元素在表头
{
head=head->next;//更新链表头
delete newNode;
}
else //不在表头
{
oldNode->next=newNode->next;
delete newNode;
}
found=true; //找到该数据
break;
}
else
{
oldNode=newNode;
newNode=newNode->next;
}
}
if(!found)cout<<"Cann't delete a nonentity information !"<<endl;
else cout<<"\n 删 除 成 功!"<<endl;
}