运算符 -- 在链表中

Operator-- in Linked List

本文关键字:链表 运算符      更新时间:2023-10-16

我正在尝试重载运算符 - 在单向列表中。

我有一个节点类,其中包含:T 信息节点类型 *链接

迭代器类(它是单链表的朋友):节点类型 *第一个节点类型 *当前布尔关闭边缘

单向链表类,具有:*第一*最后

我已经成功修改了运算符++方法,并且通过了所有测试。代码如下:

if(offTheEdge == true)
{
    return *this;
}
else
{
    if(current->link == NULL)
    {
        offTheEdge = true;
        return *this;
    }
    else
    {
        current = current->link;
    }
}
return *this;

我的指示如下:与运算符++相同,但倒退。在单向链表中向后移动意味着你必须从头开始,并确定这个>电流背后的节点。

请帮忙,无论我尝试什么,我都无法获得以前的元素并向后工作。谢谢!

我的运算符代码是:

ListIterator<T> temp;
temp.current = first;
while(temp.current->link != this->current)
{
    temp.current = temp.current->link;
}
return temp;

如果我的列表是 2,4,6,8,10,12,14,16,18,20....它每次都返回 20

据我所知,实际的迭代器在您的代码中不会更改。在你的运算符++代码中,你有current = current->link,但在运算符中,你只改变临时迭代器(即 this->current实际上从未改变过)。