重载 == 以递归方式比较两个链表
overload == to compare two linked list recursively
我有一个家庭作业问题,它要求我重载 == 运算符来比较两个链表。我需要在递归中执行此操作。
这是我的.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;
}
相关文章:
- 如何使用模板元编程在自由函数C++链接两个不相关的类
- 链接两个使用 c++ 构建的库_static
- 当我链接两个静态C++库时,我可以在两个主函数库中有两个主要功能吗?
- 在链接到两个 exe 的 dll 中共享全局变量
- 如何链接两个LLVM位码模块?
- 在没有额外代码的情况下链接两个独立类的最通用方法是什么?
- 在 Linux 上的 Visual Studio 中链接两个项目
- 在同一C++项目中链接两个不同版本的 protobuf 库
- 如何链接两个ArUco标记的姿势
- CMakeLists配置以链接两个C++项目
- 如何静态链接到两个版本的 xerces-c(或任何与此相关的库)
- Xcode 不链接两个结构
- 如何在 C/C++ 中链接两个 .so
- 链接两个具有相同函数签名的强函数符号的结果使用 G++ 以及原因
- 在 Visual Studio 解决方案中链接两个项目
- 链接两个枚举类型成员
- OpenGL - 链接两个纹理不起作用
- 我如何使用cmake链接两个库到我的程序
- 当链接到两个第三方共享库时,c++程序崩溃
- 在链接两个对象文件时,使用#ifndef防止定义一个函数两次