//重载=
template<typename T>
void chain<T>::operator=(const chain<T>&thelist)
{
listSize = thelist.listSize;
if (listSize == 0)
{//链表为空
firstNode = NULL;
return;
}
//链表theList为非空
chainNode<T>* sourceNode = thelist.firstNode; //复制目标链表的节点,相当于中间变量
firstNode = new chainNode<T>(sourceNode->element);//复制链表theList的首元素
sourceNode = sourceNode->next;//指向第二个元素
chainNode<T>* targetNode = firstNode; //当前链表的最后一个节点 ,相当于返回结果
while (sourceNode != NULL)
{//复制剩余元素
targetNode->next = new chainNode<T>(sourceNode->element);//复制第二个元素,后面依次复制
targetNode = targetNode->next;
sourceNode = sourceNode->next;
}
targetNode->next = NULL;//把最后一个指针复制,链表结束
}
单链表中重载操作符“=”
最新推荐文章于 2022-05-22 11:11:26 发布