Hello!!大家早上中午晚上好,前文我们介绍了list的使用,今天来模拟实现以下吧!
一、初步工作
1.1我们需要一个头文件来声明定义list和一个测试文件来测试List的实现效果
1.2第二步:考虑List 需要用变量、成员函数、以及实现方法
①首先List的底层是一个带头双向循环链表,所以必须要定义Node节点,节点里要有一个prev前驱指针指向前一个节点和一个next后继指针指向下一个节点,Node节点里存放数据data;
②其次链表里要有一个头结点,让头结点指向的第一个节点作为begin(),让头结点成为end(),因为头节点里是不存放数据的正好可以用作链接,图解:
③再者需要定义迭代器,begin()返回指向node1的迭代器,end()返回指向head的迭代器;且迭代器还要实现++、--、解引用、->的重载;
④对于insert、erase,insert返回插入的新节点的迭代器,erase为了防止迭代器失效返回删除的节点的下一个节点的迭代器;
二、开始实现
2.1 链表存放的节点的定义
//节点的定义(定义成模版可以接受各种类型)
template<class T>
struct list_node
{list_node<T>* _prev;list_node<T>* _next;T _data;list_node(const T&val=T()):_prev(nullptr),_next(nullptr),_data(val){}
};
2.2 链表的迭代器的封装
//迭代器
template<class T,class Ref,class Ptr>
struct _list_iterator
{typedef _list_iterator<T,Ref,Ptr> Self;typedef list_node<T> Node;Node* _node;_list_iterator():_node(nullptr){}_list_iterator(Node* node){_node = node;}_list_iterator(const Self& it){_node = it._node;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}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)const{return _node != it._node;}bool operator==(const Self& it)const{return _node == it._node;}
};
2.3 链表的定义
template<class T>class _list{typedef list_node<T> Node;public:typedef _list_iterator<T, T&, T*> iterator;typedef _list_iterator<T, const T&, const T*> const_iterator;iterator begin(){return _head->_next;}const_iterator begin()const{return _head->_next;}iterator end(){return _head;}const_iterator end()const{return _head;}void emptyinit()//对空链表的初始化{_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}_list(){emptyinit();}_list(const _list<T>& lt)// list<int> list(list2);{emptyinit();for (auto e:lt){push_back(e);}}void swap(_list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}_list<T>& operator=( _list<T> lt)// list2=list3=list4;{swap(lt);return *this;}~_list(){clear();delete _head;_head = nullptr;}void clear(){iterator it = begin();while (it != end()){it=erase(it);}_size = 0;}iterator insert(iterator pos, const T& val = T()){Node* newnode = new Node(val);Node* cur = pos._node;Node* prev = cur->_prev;prev->_next = newnode;newnode->_next = cur;cur->_prev = newnode;newnode->_prev = prev;++_size;return newnode;}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;--_size;return next;}void push_back(const T& val = T()){insert(end(), val);}void pop_back(){erase(--end());}void push_front(const T& val = T()){insert(begin(), val);}void pop_front(){erase(begin());}private:Node* _head;size_t _size;};
三、测试
3.1 尾插尾删
void test2()
{ldc::_list<int> list;list.push_back(1);list.push_back(2);list.push_back(3);list.push_back(4);list.push_back(5);list.pop_back();list.pop_back();for (auto e : list){cout << e << " ";}cout << endl;
}
3.2 头插头删
void test3()
{ldc::_list<int> list;list.push_front(1);list.push_front(2);list.push_front(3);list.push_front(4);for (auto e : list){cout << e << " ";}cout << endl;list.pop_front();list.pop_front();for (auto e : list){cout << e << " ";}cout << endl;}
3.3 迭代器访问
void test4()
{ldc::_list<int> list;list.push_back(1);list.push_back(2);list.push_back(3);list.push_back(4);ldc::_list<int>::iterator it1 = list.begin();while (it1 != list.end()){*it1 += 1;cout << *it1 << " ";++it1;}
}
3.4 const迭代器访问
void test5()
{ldc::_list<int> list;list.push_back(1);list.push_back(2);list.push_back(3);list.push_back(4);const ldc::_list<int> list2 = list;ldc::_list<int>::const_iterator it1 = list2.begin();while (it1 != list2.end()){*it1 += 1;//报错cout << *it1 << " ";++it1;}
}
以上就是list的大致功能的实现,如果对您有帮助记得点赞收藏+关注哦!!谢谢!!!
咱下期见!!!