重载 == 以递归方式比较两个链表

overload == to compare two linked list recursively

本文关键字:链表 两个 比较 递归 方式 重载      更新时间:2023-10-16

我有一个家庭作业问题,它要求我重载 == 运算符来比较两个链表。我需要在递归中执行此操作。

这是我的.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;
}

我尝试使用辅助函数来实现递归,但它仍然给出错误,指出未定义 Node。

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

谁能帮我解决这个问题?

结构 Node 是私有数据成员,在友元声明中可能不使用

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;
};

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

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

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;
}
};

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

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;
}