目录
前言:
模拟实现:
迭代器的实现:
list类功能函数实现:
初始化成空函数(empty_init):
构造函数:
拷贝构造函数:
尾插(push_back):
插入(insert):
删除(erase):
尾删(pop_back):
头插(push_front):
头删(pop_front):
清理(clear):
交换(swap):
赋值重载:
析构:
代码
前言:
在学习完list的基本功能后,我自己模拟实现了一个list,具备一些常用的基本功能,这篇博客用来分享并记录,方便后续复习。
模拟实现:
因为list中可以存很多种类型,比如int,char,float,等,还可能是自定义类型,所以我打算使用类模板,先把简单的节点弄成类模板:
接下来就是list的类模板:
迭代器的实现:
这里迭代器的模拟实现不能像vector一样简单的使用原生指针,因为链表的地址不是连续的,我们在进行原生指针的++或者--操作时,是无法实现访问下一个或者上一个元素的,那该怎样实现简单的对迭代器++或者--就能实现访问下一个或者上一个元素呢?
这里有一个巧妙地方法就是借助类,没错,我们将原生指针放入一个名为Listiterator的类里面,然后在这个类中,使用运算符重载,重载++,--,*,->等运算符,就能像库里面一样使用迭代器了。
上图的Ref和Ptr模板分别是传引用和传指针,用于应对const 迭代器的使用 ,保证const迭代器可以修改迭代器,但不能修改该迭代器指向的内容。接下来开始在这个类中重载各种运算符:
这几个运算符重载都很简单,应该都能看懂,接下来进入list类模板中,就行list的功能函数实现:
list类功能函数实现:
先来几个简单但又很重要的函数实现:
初始化成空函数(empty_init):
构造函数:
有了上面这个函数后,构造函数就简单了,直接复用即可:
拷贝构造函数:
尾插(push_back):
插入(insert):
删除(erase):
尾删(pop_back):
头插(push_front):
头删(pop_front):
清理(clear):
交换(swap):
赋值重载:
此处传值传参的妙处:
list1=iist2,进入函数此时lt是list2的拷贝,因为swap是成员函数,所以有一个隐含的this指针,此时只需传参lt就可以完成lt和list1交换,间接完成对list1的赋值,同时没有改变list2,只是改变了lt,而lt出作用域后就会消失。
析构:
代码:
#define _CRT_SECURE_NO_WARNINGS 1 #includeusing namespace std; namespace sxk { template struct ListNode { ListNode * next;//下一个节点的地址 ListNode * prev;//上一个节点的地址 T val;//数据 ListNode(const T& x = T())//节点的构造函数 :next(nullptr) , prev(nullptr) , val(x) {} }; template struct Listiterator { typedef ListNode Node; typedef Listiterator Self; typedef Listiterator iterator; typedef Listiterator const_iterator; Node* _node; Listiterator(Node* node)//迭代器的构造函数 :_node(node) {} Ref operator*() { return _node->val; } Ptr operator->() { return &_node->val; } Self& operator++() { _node = _node->next; return *this; } Self operator++(int) { Self tmp(*this); _node = _node->next; return tmp; } Self& operator--() { _node = _node->prev; return *this; } Self operator--(int) { Self tmp(*this); _node = _node->prev; return tmp; } bool operator==(const Self& it) { return _node == it._node; } bool operator!=(const Self& it) { return _node != it._node; } }; template class list { public: typedef ListNode Node; typedef Listiterator iterator; typedef Listiterator const_iterator; iterator begin() { return _head->next; } iterator end() { return _head; } const_iterator begin()const { return _head->next; } const_iterator end()const { return _head; } size_t size() { return _size; } bool empty() { return _size == 0; } void empty_init() { _head = new Node;//new出头节点 _head->next = _head;//头节点下一个指向自己 _head->prev = _head;//头节点上一个指向自己 _size = 0; } list()//构造函数 { empty_init(); } list(const list & lt)//拷贝构造函数 { empty_init();//先初始化成只有一个头节点 for (auto& x : lt) { push_back(x);//直接尾插即可 } } list & operator=(const list lt)//lt是赋值类的拷贝 { swap(lt);//交换lt和this,可以完成赋值并不影响赋值类 return *this; } void swap(const list & lt) { std::swap(_head, lt._head);//直接调用库里的swap交换两个成员变量即可 std::swap(_size, lt._size); } void clear() { iterator it = begin(); while (it != end())//遍历删除 { it = erase(it);//更新it,防止erase后迭代器失效 it++; } } ~list() { clear();//先清理,只保留一个头节点 delete _head;//释放头节点 _head = nullptr; } void push_back(const T& x) { Node* newnode = new Node;//new出新节点 newnode->val = x;//给新节点赋值 Node* tail = _head->prev;//记录尾节点 tail->next = newnode;//尾节点的下一个指向新节点 newnode->next = _head;//新节点的next指向头节点 newnode->prev = tail;//新节点的prev指向之前旧的尾节点 _head->prev = newnode;//头节点的prev指向新节点 _size++; } void push_front() { insert(begin()); } void pop_back() { erase(--end());//直接复用erase,注意end指向的是头节点,所以要-- } void pop_front() { erase(begin()); } void insert(iterator pos, const T& x)//在pos位置前插入x { Node* newnode = new Node;//new出新节点 newnode->val = x;//给新节点赋值 Node* cur = pos._node;//记录当前pos位置 Node* prev = cur->prev;//记录pos前一个 prev->next = newnode;//pos前一个节点的next指向新节点 newnode->next = cur;//新节点的next指向pos节点 newnode->prev = prev;//新节点的prev指向pos前一个节点 cur->prev = newnode;//pos的prev指向新节点 _size++; } iterator erase(iterator pos)//删除pos位置的值 { Node* cur = pos._node;//记录pos位置的节点 Node* prev = cur->prev;//记录pos的前一个节点 Node* next = cur->next;//记录pos的下一个节点 prev->next = cur->next;//pos的前一个节点的next指向pos的下一个节点 next->prev = prev;//pos的下一个节点的prev指向pos的前一个节点 delete cur;//释放pos位置的节点 cur = nullptr;//置为空 _size--; return iterator(next);//防止erase后迭代器失效,更新迭代器 } private: Node* _head; size_t _size; }; void Print_List(const list & lt) { list ::const_iterator it = lt.begin(); while (it != lt.end()) { cout << (*it) << " "; it++; } cout << endl; } }