双链表实现一个类似STL的List类
2012-06-23 13:26
363 查看
#ifndef MyList_H #define MyList_H template<typename Object> class MyList{ private: struct Node{ Object data; Node *prev; Node *next; Node(const Object &d = Object(), Node *p = NULL, Node *n = NULL): data(d),prev(p),next(n){} }; int theSize; Node *head; Node *tail; void Init(){ theSize = 0; head = new Node; tail = new Node; head->next = tail; tail->prev = head; } public: class const_iterator{ protected: Node *current; const_iterator(Node *p):current(p){} Object& retrieve()const{ return current->data; } friend class MyList<Object>; public: const_iterator():current(NULL){} const Object& operator*() const{ return current->data; } const_iterator& operator++(){ current = current->next; return *this; } const_iterator operator++(int){ const_iterator p(current); current = current->next; return p; } bool operator==(const const_iterator& rhs) const{ return current==rhs.current; } bool operator!=(const const_iterator& rhs)const{ return current!=rhs.current; } }; class iterator : public const_iterator{ protected: friend class MyList<Object>; iterator(Node* p):const_iterator(p){} public: iterator(){} Object& operator*(){ return current->data; } const Object& operator*()const{ const_iterator::operator*(); } iterator& operator++(){ current = current->next; return current->data; } iterator& operator--(){ current = current->prev; return *this; } iterator operator++(int){ iterator p(current); current = current->next; return p; } }; public: MyList() { Init(); } MyList(const MyList& rhs){ Init(); *this = rhs; } ~MyList(){ clear(); delete head; delete tail; } const MyList& operator=(const MyList &rhs){ if(this != &rhs){ theSize = rhs.theSize; clear(); for(const_iterator it = rhs.begin(); it != rhs.end(); it++) push_back(*it); } return *this; } iterator begin(){ return iterator(head->next); } const_iterator begin() const{ return const_iterator(head->next); } iterator end(){ return iterator(tail); } const_iterator end()const{ return const_iterator(tail); } int size() const{ return theSize; } bool empty()const{ return theSize==0; } void clear(){ while(!empty()){ pop_front(); } } Object& front(){ return begin()->data; } const Object& front() const{ return begin()->data; } Object& back(){ return (--end())->data; } const Object& back()const{ return (--end())->data; } void push_front(Object& obj){ insert(begin(),obj); } void push_back(Object& obj){ insert(end(),obj); } void pop_front(){ erase(begin()); } void pop_back(){ erase(--end()); } iterator insert(iterator it, const Object &obj){ Node *node = new Node(obj); node->next = it.current; node->prev = it.current->prev; it.current->prev->next = node; it.current->prev = node; theSize++; iterator retVal(node); return retVal; } iterator erase(iterator it){ Node *p = it.current; p->prev->next = p->next; p->next->prev = p->prev; iterator retVal(p->next); delete p; theSize--; return retVal; } iterator erase(iterator start,iterator end){ while(start != end){ start = erase(start); } return end; } }; #endif
测试:
相关文章推荐
- 实现一个类似STL的Vector类
- 自己实现的一个list双链表
- HanList<T> 实现 类似stl:list<T>
- 实现一个栈类,类似STL中的栈
- 定义一个List集合并实例化对应的实现类对象。循环输入任意个整数,输入0结束输入循环,对集合中的数据进行从大到小的排序后输出
- 【C++/STL】list的实现(采用空间配置器和迭代器)
- 实现一个类似Chrome新功能提示的popoup
- java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制)
- iOS 实现一个类似电商购物车界面示例
- STL中list的使用及模拟实现
- Sench类似List多列控件实现
- STL中list的sort算法的实现原理
- CSharpGL(35)用ViewPort实现类似3DMax那样的把一个场景渲染到4个视口
- java数据结构与算法之改良顺序表与双链表类似ArrayList和LinkedList(带Iterator迭代器与fast-fail机制)
- 最近被一个问题困扰着,如何实现类似Photoshop的多层图像操作,每层可单独操作,又可以组合显示出来。
- STL-模拟实现list
- Mac下用vue-cli+webpack+bootstrap实现一个todo-list(一)
- 如何实现Java中一个简单的LinkedList
- LinkedList双链表简单分析,及单链表的实现代码