首先我们了解STL的基本概念:STL不仅仅是一个可复用的组件库,而且是一个包含算法和数据结构的软件框架
STL有六大组件:
1,容器---各种数据结构(vector,list,map,set)
2,迭代器---扮演容器和算法的胶合剂
3,空间分配器---负责内存空间的分配与管理
4,配接器---一种修饰容器或者仿函数或者迭代器接口的东西
5,算法---各种常见算法(sort,search,copy等)
6,仿函数
模拟实现双向循环列表有两个核心,一是实现它的结构,带头节点的双向链表,它会让逻辑更简单。二是迭代器,其实就是一个带节点的指针,只是它用了一个类把它封装起来,然后重载了一系列的运算符。
模拟实现STL之双向list:
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
struct MyListNode
{
MyListNode<T>* _prev;
MyListNode<T>* _next;
T _data;
MyListNode(const T& x)//节点的构造函数
:_data(x)
, _next(NULL)
, _prev(NULL)
{}
};
template<class T, class Ref, class Ptr>
struct ListIterator//迭代器
{
typedef MyListNode<T> Node;
Node* _node;
typedef ListIterator<T, Ref, Ptr> Self;
ListIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
Self& operator++()//后置++
{
_node = _node->_next;
return *this;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self operator++(int)//前置++
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev;
return _tmp;
}
bool operator != (const Self& s)const
{
return _node != s._node;
}
};
template<class T>
class List
{
public:
typedef MyListNode<T> Node;
typedef ListIterator<T, T&, T*> Iterator;//const 型迭代器和非const型
typedef ListIterator<T, const T&, const T*> ConstIterator;
List()
{
_head = BuyNode(T());//用匿名对象初始化
_head->_next = _head;
_head->_prev = _head;
}
~List()
{
Clear();
delete _head;
_head = NULL;
}
void Clear()
{
Node* cur = _head->_next;
while (cur != _head)
{
Node* next = cur->_next;//防止迭代器失效
delete cur;
cur = next;
}
}
Iterator Begin()
{
return _head->_next;
}
Iterator End()
{
return Iterator(_head);
}
ConstIterator Begin()const
{
return ConstIterator(_head->_next);
}
ConstIterator End()const
{
return ConstIterator(_head);
}
void PushBack(const T& x)
{
Node* tail = _head->_prev;
Node* tmp = BuyNode(x);
tail->_next = tmp;
tmp->_prev = tail;
_head->_prev = tmp;
tmp->_next = _head;
}
void PopBack()//尾删
{
Iterator tmp = End();
Erase(--tmp);
}
void PushFront(const T& x)//头插
{
Insert(Begin(), x);
}
void PopFront()//头删,不能删除头结点
{
Erase(Begin());
}
Iterator Insert(Iterator pos, const T& x)//任意位置插入
{
Node* tmp = BuyNode(x);
Node* next = pos._node->_next;
Node* prev = pos._node->_prev;
prev->_next = tmp;
tmp->_prev = prev;
tmp->_next = next;
next->_prev = tmp;
return tmp;
}
Iterator Erase(Iterator pos)//任意位置删除
{
Node* next = pos._node->_next;
Node* prev = pos._node->_prev;
prev->_next = next;
next->_prev = prev;
delete pos._node;
return next;
}
protected:
Node* BuyNode(const T& x)
{
return new Node(x);
}
private:
Node* _head;
};
void PrintMylst(const List<int>& l1)
{
List<int>::ConstIterator it1 = l1.Begin();
while (it1 != l1.End())
{
cout << *it1 << " ";
++it1;
}
cout << endl;
}
void Test()
{
List<int> l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.PushFront(5);
l1.PushFront(2);
/*l1.PopFront();
l1.PopFront();*/
PrintMylst(l1);
}
int main()
{
Test();
system("pause\n");
return 0;
}