模拟实现list(迭代器)
来源:互联网 发布:mac安装win7双系统 编辑:程序博客网 时间:2024/06/05 19:27
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; ListNode<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;};
阅读全文
0 0
- 模拟实现list(迭代器)
- 模拟实现list(迭代器)
- STL模拟实现List+迭代器
- 【STL】迭代器与List的模拟实现
- 【STL】list迭代器的模拟实现
- 迭代器模拟实现STL中的list
- 模拟实现容器List(迭代器)
- <STL>模拟实现List
- C++::模拟实现List
- STL-模拟实现List
- 模拟实现 list
- list模拟实现
- 【STL】模拟实现list
- 模拟实现list(iterator)
- list简单模拟实现
- 模拟实现list
- 数据结构::模拟STL实现list
- 模板类模拟实现List
- lesson26 用switch语句解决问题
- JVM原理
- SQL中EXISTS的用法
- 文章标题4.3(2)
- 线程--Thread和Runnable创建线程的区别
- 模拟实现list(迭代器)
- 三值排序(计蒜客)
- Android studio安装的问题
- VS2005、VS2008为类添加成员时显示 “length”为空或不是对象的处理方法
- Android Studio在创建项目是一直处于building “project name”gradle project info 解决方案
- Maven 打包时加入本地依赖包
- Android BaseAdapter记录
- lucence in action读后感
- 分布式数据库中的CAP原理 CAP+BASE