双链表–迭代器、重载方法、使用类重写链表方法
1: 双向链表的节点类
- 1:数据成员:数据域/前驱指针/后继指针
- 2:四大默认成员函数:构造/析构/拷贝构造/赋值
- 3:成员函数
- 4:新增功能
Node.h
class Node
{
public :
friend class List;
friend class Iterator;
Node() :pFront(nullptr), pNext(nullptr)
{
}
Node(int val, Node* pFront=nullptr, Node* pNext=nullptr);
~Node();
private:
int val;
Node* pFront;
Node* pNext;
};
Node.cpp
#include "Node.h"
Node::Node(int val, Node* pFront, Node* pNext):val(val),pFront(pFront),pNext(pNext)
{
}
Node::~Node()
{
pFront = nullptr;
pNext = nullptr;
}
2: 双想链表迭代器类–重载
- 迭代器指向的节点: Node* pNode;
- 构造函数:Iterator(Node* pNode);
- 友元类:friend class List;
2.1构造函数
Iterator::Iterator(Node* pNode):pNode(pNode)
{
}
2.2重载后置++
Iterator Iterator::operator++(int)
{
Iterator temp = *this;
pNode = pNode->pNext;
return temp;
}
2.3重载前置++
Iterator& Iterator::operator++()
{
this->pNode = pNode->pNext;
return *this;
}
2.4重载后置–
Iterator Iterator::operator--(int)
{
Iterator it = *this;
this->pNode = pNode->pFront;
return it;
}
2.5重载前置–
Iterator& Iterator::operator--()
{
this->pNode = pNode->pFront;
return *