#include <iostream>
#include <assert.h>
using namespace std;
template <class T>
//双向链表
struct ListNode{
ListNode(const T& val = T())
:_next(nullptr)
,_prev(nullptr)
,_val(val)
{}
ListNode<T>* _next;
ListNode<T>* _prev;
T _val;
};
//迭代器
template<class T,class Ref,class Ptr>
struct ListIterator{
typedef ListNode<T>* pNode;
typedef ListIterator<T,Ref,Ptr> self;
pNode _node;
//构造函数
ListIterator(pNode node)
:_node(node)
{}
//重载+
self operator+(const T& n){
for (int i = 0; i < n; i++){
_node = _node->_next;
}
return *this;
}
//重载前置++
self operator++(){
_node = _node->_next;
return *this;
}
//重载后置++,返回没有改变之前的值,然后自身++
self operator++(int){
self tmp(*this);
_node = _node->_next;
return *this;
}
//重载前置--
self& operator--(){
_node = _node->_prev;
return *this;
}
//重载后置--
self operator--(int){
self tmp(*this);
_node = _node->_prev;
return *this;
}
//重载*
Ref operator*(){
return _node->_val;
}
//重载->
Ptr operator->(){
return &(operator*());
}
//重载!=,比较两个迭代器封装的结点是否一样
bool operator!=(const self l){
return _node != l._node;
}
//重载==
bool operator==(const self l){
return _node == l._node;
}
};
template<class T>
class List{
public:
typedef ListNode<T> Node;
typedef ListNode<T>* pNode;
//const对象不能调用非const对象成员函数,只能调用const成员函数
//但是const成员函数operator++,operator--不能改变_node的值
//导致const不能执行++,--
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
//构造函数
List(const T& val = T())
:_head(new Node(val))
{
_head->_prev = _head;
_head->_next = _head;
}
//拷贝构造函数
//list需要实现深拷贝,否则浅拷贝只会拷贝对象模型中的头指针,不会拷贝
//结点,析构函数释放结点的时候会造成二次释放,导致程序崩溃
List(const List<T>& l){
//首先创建头指针
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
//遍历一遍,插入到list对象中去
for (auto e : l){
Push_Back(e);
}
}
//赋值运算符
List<T>& operator=( List<T>& l){
传统写法
//if (this != &l){
// Clear();
// for (const auto& e : l){
// Push_Back(e);
// }
//}
//return *this;
swap(_head, l._head);
return *this;
}
//尾插,更新指向
void Push_Back(const T& val ){
pNode CurNode = new Node(val);
assert(CurNode);
pNode prev = _head->_prev;
prev->_next = CurNode;
CurNode->_prev = prev;
CurNode->_next = _head;
_head->_prev = CurNode;
}
//头插
void PushFront(const T& val){
Insert(begin(), val);
}
//头删
void PopFront(){
Erase(begin());
}
//尾删
void PopBack(){
Erase(--end());
}
//迭代器
iterator begin(){
return iterator(_head->_next);
}
iterator end(){
return iterator(_head);
}
//const迭代器
const_iterator begin()const{
return const_iterator(_head->_next);
}
const_iterator end()const{
return const_iterator(_head);
}
//在pos前面插入val
void Insert(iterator pos, const T& val){
pNode NewNode = new Node(val);
pNode cur = pos._node;
pNode prev = cur->_prev;
prev->_next = NewNode;
NewNode->_prev = prev;
NewNode->_next = cur;
cur->_prev = NewNode;
}
//删除指定结点的值
//Erase:迭代器失效
//获取Erase的返回值(当前被删除结点的下一个位置),更新迭代器
iterator Erase(iterator pos){
if (pos != end()){
pNode cur = pos._node;
pNode prev = cur->_prev;
pNode next = cur->_next;
prev->_next = next;
next->_prev = prev;
delete cur;
//更新pos的位置
pos = iterator(next);
}
return pos;
}
//删除全部
void Clear(){
if (_head){
pNode cur = _head->_next;
while (cur != _head){
pNode next = cur->_next;
delete cur;
cur = next;
}
_head->_next = _head;
_head->_prev = _head;
}
}
//析构函数
~List(){
Clear();
if (_head){
delete _head;
_head = nullptr;
}
}
private:
pNode _head;
};
//构建date类
//class Date{
// int _year;
// int _month;
// int _day;
//};
template<class T>
void PrintList(const List<T>& l){
auto lit = l.begin();
while (lit != l.end())
{
//*lit = 10;
cout << *lit << " ";
++lit;
}
cout << endl;
}
int main(){
List<int> l;
l.Push_Back(2);
l.Push_Back(3);
l.Push_Back(4);
l.Push_Back(5);
l.Push_Back(6);
//l.PopBack();
//l.Insert(l.begin(), 5);
l.Clear();
//PrintList(l);
//l.Erase(l.begin()+2);
//PrintList(l);
//l.PushFront(6);
//l.PopFront();
auto lit = l.begin();
while (lit != l.end())
{
//*lit = 10;
cout << *lit << " ";
++lit;
}
/*PrintList(l);*/
cout << endl;
system("pause");
return 0;
}
list的模拟实现(C++)
最新推荐文章于 2024-05-07 22:09:24 发布