【问题标题】:overload == to compare two linked list recursively重载 == 递归比较两个链表
【发布时间】:2020-01-25 23:53:50
【问题描述】:

我有一个作业问题,要求我重载 == 运算符来比较两个链表。我需要在递归中做到这一点。

这是我的 .h 文件

class  LList {
   public:
      friend bool operator == (const LList& lfSide, const LList& rtSide);
   private:
      struct Node {
         int item;
         Node* next;
      };
      friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);
      Node* head;
}

我尝试使用辅助函数来进行递归,但它仍然给出错误说节点未定义。

friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);

谁能帮我解决这个问题?

【问题讨论】:

  • 您希望如何使用 4 个操作数调用 operator==?您可能应该将其设为非运算符函数。

标签: c++ recursion linked-list overloading friend


【解决方案1】:

这可能会对您有所帮助。 我通过将一些成员变量设为公开来保持简单。

class Node
{
public:
    int m_Value = -1;
    Node* m_Next = nullptr;
    Node()
    {
        m_Value = -1;
        m_Next = NULL;
    }
};

//Linked List class
class LinkedList
{
    //Head Node
    Node* m_Head;
    //Tail Node
    Node* m_Last;

public:
    //Constructor
    LinkedList()
    {
        m_Head = nullptr;
        m_Last = nullptr;
    }
    //Add value to list
    void AddValue(int value)
    {
        if (m_Head == nullptr)
        {
            m_Head = new Node();
            m_Head->m_Value = value;
            m_Last = m_Head;
        }
        else
        {
            Node* newNode = new Node();
            newNode->m_Value = value;
            m_Last->m_Next = newNode;
            m_Last = newNode;
        }
    }

    //Display the elements of the list
    void display()
    {
        Node* travelNode = m_Head;
        while (travelNode != nullptr)
        {
            cout << travelNode->m_Value << " ==> ";
            travelNode = travelNode->m_Next;
        }
    }

    //Compare function
    bool operator==(const LinkedList& List)
    {
        return CompareList(this->m_Head, List.m_Head);
    }

    bool CompareList( Node* LeftList,  Node* rightList)
    {
        //If both nodes reaches to end then it is equal
        if (LeftList == nullptr && rightList == nullptr) return true;

        //If one of the node has more nodes then list are not equal
        if (LeftList == nullptr || rightList == nullptr) return false;

        //Compare the values
        if (LeftList->m_Value != rightList->m_Value) return false;

        //Call recursion
        CompareList(LeftList->m_Next, rightList->m_Next);
    }
};

int main()
{
    //First List
    LinkedList list1;
    list1.AddValue(1);
    list1.AddValue(2);
    list1.AddValue(3);
    list1.AddValue(4);
    list1.AddValue(5);

    //list1.display();

    //Second List
    LinkedList list2;
    list2.AddValue(1);
    list2.AddValue(2);
    list2.AddValue(3);
    list2.AddValue(4);
    list2.AddValue(5);

    //list2.display();

    //Compare
    bool areEq = list1 == list2;
    cout << "Are Equal " << areEq << endl;
}

【讨论】:

    【解决方案2】:

    另一种方法是只将一个参数传递给 operator ==,因为 this 是隐式传递的。这里是完整的代码

    template <typename T>
    struct Node
    {
        T data;
        Node<T>* next;
    };
    template <class T>
    class LinkedList
    {
    private:
        Node<T>* head;  
    
        bool internalCompare(Node<T>* first, Node<T>* second)
        {
            if((first == nullptr) && (second == nullptr))
            {
                return true;
            }
            else if(((first == nullptr) && (second != nullptr)) || ((first != nullptr) && (second == nullptr)))
            {
                return false;
            }
            else if (first->data != second->data)
            {
                return false;
            }
            return internalCompare(first->next,second->next);
        }
    public:
        LinkedList() :head(nullptr) {}      
        bool operator ==(LinkedList<T> & second)
        {
            if(internalCompare(head,second.head))
            {
                return true;
            }
            return false;
        }
    };
    

    【讨论】:

    • 现在它的标识符 internalCompare 是未定义的。
    • @Keric,查看之前评论中的链接以获取完整示例
    【解决方案3】:

    结构节点为私有数据成员,不可在好友声明中使用

    friend bool operator == (const LList& lfSide, Node* headlf, const LList& rtSide, Node* headrt);
    

    你可以像这样定义结构

    #include <iostream>
    class  LList {
       public:
          friend bool operator == (const LList& lfSide, const LList& rtSide);
       private:
          struct Node {
             int item;
             Node* next;
             bool operator ==( const Node &headrt);
          };
          Node* head;
    };
    

    并且在友元运算符中使用结构节点的运算符 ==。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2015-06-14
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多