模拟实现list(迭代器)
2017-10-23 23:08
453 查看
list同vector类似,都是库提供的众多容器中的一个。同时list 以模板方式实现,可以处理任意型别的变量,包括使用者自定义的资料型态。
在编程语言中List 是标准类库中的一个类,可以简单视之为双向链表,以线性列的方式管理物件集合。和vector不一样的是,list不支持对元素的任意存取。list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效。
在这里以双向带头链表实现:
在编程语言中List 是标准类库中的一个类,可以简单视之为双向链表,以线性列的方式管理物件集合。和vector不一样的是,list不支持对元素的任意存取。list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效。
在这里以双向带头链表实现:
using namespace std; #include <iostream> #include <assert.h> //双向有头链表 template <class T> class ListNode { public: ListNode() :_data(NULL) {} ListNode(const T&x) :_data(x) ,_next(NULL) ,_prev(NULL) {} public: T _data; ListNode<T> * _next; L 4000 istNode<T> * _prev; }; template <class T, class Ref, class Ptr> class ListIterator { public: typedef ListNode<T> Node; typedef ListIterator<T, Ref, Ptr> Self; ListIterator() :_node(0) {} ListIterator(Node *node) :_node(node) {} Ref operator *() { return _node->_data; } Ptr operator->(){ return &(_node->_data); } Self &operator++() { _node = _node->_next; return *this; } Self operator++(int) { Self temp = _node; _node = _node->_next; return Self(temp); } Self &operator--() { _node = _node->_prev; return *this; } Self operator--(int) { Self temp = _node; _node = _node->_prev; return Self(temp); } bool operator!=(const Self& s) const { return this->_node != s._node; } bool operator==(const Self& s) const { return this->_node == s._node; } public: Node *_node; }; template <class T> class List { public: typedef ListIterator<T, T&, T*> Iterator; typedef ListNode<T> Node; public: List() //构造 :_head(new ListNode<T>) { _head->_next = _head; _head->_prev = _head; } List(const List&l) //拷贝构造 :_head(new ListNode<T>) { _head->_next = _head; _head->_prev = _head; Node *front = l._head->_next; Node* tail = l._head->_prev; while (front != tail) { PushBack(front->_data); front = front->_next; } } List &operator = (const List &l) { //赋值运算符重载 List<T> *nHead = new ListNode<T>; Node *front = l._head->_next; Node* tail = l._head->_prev; while (front != tail) { nHead->PushBack(front->_data); } Clear(); List<T> nHead; _head = nHead; } List(const T *Array, size_t size) // :_head(new ListNode<T>) { _head->_next = _head; _head->_prev = _head; for (size_t i = 0; i < size; i++) { PushBack(Array[i]); } } ~List() { if (_head) { delete _head; } } public: void PushBack(const T &t) //尾插 { Node *tail = _head->_prev; Node *temp = new Node(t); tail->_next = temp; temp->_prev = tail; _head->_prev = temp; temp->_next = _head; } void PopBack() { //尾删 assert(_head != _head->_next); //空链 Node *tail = _head->_prev; // _head->_prev = tail->_prev; tail->_prev->_next = _head; delete tail; } void PushFront(const T&t){ //头插 Node *cur = new Node(T); cur->_next = _head->_next; cur->_prev = _head; _head->_next->_prev = cur; _head->_next = cur; } void PopFront() { //头删 assert(_head != _head->_next); Node *del = _head->_next; _head->_next = del->_next; del->_next->_prev = _head; delete del; } List& Back() { return _head->_prev; } size_t Size() { int count = 0; Node *cur = _head; while (cur != _head->_prev) { count++; cur = cur->_next; } return count; } bool Empty() { return _head == _head->_next; } public: Iterator Begin() { return Iterator(_head->_next); } Iterator End() { return Iterator(_head->_prev); } Iterator Find(const T &t) { Node* front = _head->_next; Node* tail = _head->_prev; while (front != tail) { if (front->_data == t) return Iterator(front); front=front->_next; } return NULL; } Iterator Erase(Iterator pos) { //定点删除 Node *next = pos._node->_next; pos._node->_prev->_next = next; next->_prev = pos._node->_prev; delete pos._node; return Iterator(next); } Iterator Insetr(Iterator pos, const T&x) { //定点插入 Node *next = pos._node->_next; Node *cur = pos._node; Node *temp = new Node(x); cur->_next = temp; temp->_prev = cur; temp->_next = next; next->_prev = temp; return Iterator(temp); } void Clear() { Node *front = _head->_next; Node *tail = _head->_prev; Node *del; while (front != tail) { del = front; delete del; front = front->_next; } } T Top() { if(!Empty()) return _head->_next->_data; return NULL; } private: Node *_head; };
相关文章推荐
- STL容器中list与迭代器iterator的模拟实现
- 模拟实现list(迭代器)
- 迭代器模拟实现STL中的list
- C++模拟实现容器list(含迭代器)
- 【STL】迭代器与List的模拟实现
- C++ 模拟实现list(迭代器)实现代码
- 模拟实现STL中list(用迭代器)
- 模拟实现容器List(迭代器)
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- 类模板模拟实现STL中List
- 模拟实现STL中的list
- list模拟实现
- c#利用链表模拟实现泛型集合List﹤T﹥
- java模拟LinkedList实现双链表,完成基本操作
- 以模板模拟实现List容器
- 设计模式(8) - 迭代器模式(iterator)- 实现ArrayList和linkedList的迭代器
- [C++]List with iterator(链表的迭代器实现)
- 模板类模拟实现List
- JAVA容器-模拟LinkedList实现(双链表)
- 循环双链表之Java实现(模拟JDK中LinkedList)