在前面我们用C++中的类来实现了双向链表,C++实现双向链表今天我们用模板类来实现各种可能出现的类型的带头双向链表(容器List)。
该链表不仅是带头的双向链表,而且头节点与尾结点构成了循环链表
代码如下:
/*
#pragma once
#include
#include
#include
using namespace std;
template<class T>
struct ListNode
{
public:
ListNode(const T& data = 0)
:_data(data)
,prev(0)
,next(0)
{}
public:
T _data;
ListNode<T>* prev;
ListNode<T>* next;
};
template<class T,class Ref,class Pre>
class _ListIterator_
{
public:
typedef _ListIterator_<T,Ref,Pre> self;
typedef ListNode<T>* LinkType;
typedef Ref Reference;
typedef Pre Pointer;
typedef _ListIterator_<T,T&,T*> Iterator;
public:
_ListIterator_(LinkType x = 0)
{}
_ListIterator_(const Iterator& x)
:_node(x._node)
{}
bool operator==(const Iterator& x)
{
return _node = x._node;
}
bool operator!=(const Iterator& x)
{
return _node!= x._node;
}
T& operator*()
{
return ((*_node)._data);
}
Pointer operator->()
{
return &(LinkType*());
}
self& operator++()
{
_node = _node->next;
return *this;
}
self operator++(int)
{
LinkType pTemp = _node;
_node = _node->next;
return *pTemp;
}
self& operator--()
{
_node = _node->prev;
return *this;
}
self operator--(int)
{
LinkType pTemp = _node;
_node = _node->prev;
return *pTemp;
}
public:
LinkType _node;
};
template<class T>
class List
{
public:
typedef ListNode<T> Node;
typedef Node& Reference;
typedef size_t SizeType;
typedef Node* LinkNode;
typedef _ListIterator_<T,T&,T*> Iterator;
typedef _ListIterator_<T,const T&,const T*> ConstIterator;
public:
List()
{
EmptyInit();
}
List(SizeType n,const T& data)
{
EmptyInit();
Iterator pTempNode = _node;
for(size_t idx = 0;idx < n; ++idx)
{
LinkNode _NewNode = new Node;
_NewNode->_data = data;
_NewNode->next = _node;
_NewNode->prev = pTempNode._node;
*(pTempNode._node->next) = *_NewNode;
*(_node->prev)= *_NewNode;
++pTempNode;
}
}
List(const List<T>& l)
{
EmptyInit();
Iterator pTempNode = _Node;//指向新建链表
Iterator PTempNode = l._node;//指向链表l
for(size_t idx = 0;idx < n; ++idx)
{
++PTempNode;
Iterator _NewNoed = new Node;
_NewNode.node->_data = PTempNode._node->_data;
_NewNode.node->next = *_node;
_NewNode.node->prev = *pTempNode._node;
pTempNode->next = *_NewNode._node;
_node->prev = *_NewNode._node;
++pTempNode;
}
}
List& operator=(const List<T>& l)
{
Iterator it1 = _node;
Tierator it2 = l._node;
if(size() >= l.size())
{
for(size_t idx = 0;idx < l.size(); ++idx)
{
it1++;
it2++;
it1._node->_data = it2._node->_data;
}
}
else
{
it1 = _node;
it2 = l._node;
for(size_t idx = 0;idx < size(); ++idx)
{
it1++;
it2++;
it1._node->_data = it2._node-_data;
}
for(size_t idx = 0;idx<l.size()-size();++idx)
{
++it2;
Iterator _NewNode = new Node;
_NewNode._code->_data = t2._node->data;
_NewNode._code->next = *_node;
_NewNode._code->prev = *(it1._code);
it1._code->next = *_NewNode._code;
_node->prev = *NewNode._code;
it1++;
}
}
}
//~List()
//{
// Iterator it =--End();
// while(it != _node)
// {
// delete it._code;
// --it;
// }
// delete _node;
//}
///
Iterator Begin()
{
return _node->next;
}
ConstIterator Begin()const
{
return _node->next;
}
bool Empty()
{
return _node==_node->next;
}
Iterator End()
{
return _node;
}
ConstIterator End()const
{
return _node;
}
SizeType size()const
{
SizeType n = 0;
Iterator it = _node->next;
while(it < End())
{
n++;
++it;
}
return n;
}
SizeType MaxSize()const
{
return SizeType(-1);
}
Reference Front()
{
return *(Begin());
}
const Reference Front()const
{
return *(Begin());
}
Reference Back()
{
return *(End()->prev)
}
const Reference Back()const
{
return *(End()->prev)
}
//
Iterator Insert(Iterator pos, const T& x = T())
{
Iterator _NewNode = new Node;
_NewNode->data = x;
_NewNode._code->next = *pos._node;
_NewNode._code->prev = *(pos._node->prev);
pos._code->prev->next = *_NewNode._node;
pos._code->prev = *_NewNode._code;
}
void PushFront(const T& x)
{
Iterator _NewNode = new Node;
_NewNode->data = x;
_NewNode._code->next = *(_node->next);
_NewNode._code->prev = *(_node);
_node->next->prev = *_NewNode._node;
_node->prev = *_NewNode._node;
}
void PushBack(const T& x)
{
Iterator it = --End();
LinkNode _NewNode = new Node;
_NewNode->_data = x;
*(_NewNode->next) = *(_node);
*(_NewNode->prev) = *(it._node);
*(it._node->next) = *(_NewNode);
*(_node->prev) = *(_NewNode);
}
Iterator Erase(Iterator pos)
{
pos._code->prev = *(pos._code->next);
pos._code->next = *(pos._code->prev);
delete pos;
}
void PopFront()
{
Erase(_node->next);
}
void PopBack()
{
Erase(--End());
}
void ReSize(SizeType n, const T& data)
{
if(n <= size())
{
for(size_t idx=0; idx<size()-n; ++idx)
{
PopBack();
}
}
else
{
for(size_t idx=0; idx<size()-n; ++idx)
{
PushBack(data);
}
}
}
void Assign(SizeType n, const T& data)
{
Iterator it = _node;
if(n <= size())
{
for(size_t idx=0; idx<n; ++idx)
{
++it;
it._code->data = data;
}
}
else
{
for(size_t idx=0; idx<size(); ++idx)
{
++it;
it._code->data = data;
}
for(size_t idx=0; idx<n-size(); ++idx)
{
PushBack(data);
}
}
}
void Clear()
{
Iterator it =--End();
while(it != _node)
{
delete it._code;
--it;
}
_node->next = _node->prev = NULL;
}
private:
void EmptyInit()
{
_node = new Node;
_node->prev = _node;
_node->next = _node;
}
protected:
LinkNode _node;
};
*/
#pragma once
#include
#include
using namespace std;
template<class T>
struct ListNode
{
ListNode(const T& data = T())
: _prev(0)
, _next(0)
, _data(data)
{}
ListNode<T>* _prev;
ListNode<T>* _next;
T _data;
};
template<class T, class Ref, class Ptr>
class __ListIterator__
{
typedef __ListIterator__<T, T&, T*> Iterator;
typedef __ListIterator__<T, const T&, const T*> ConstIterator;
typedef __ListIterator__<T, Ref, Ptr> Self;
typedef ListNode<T>* LinkType;
typedef Ref Reference;
typedef Ptr Pointer;
public:
__ListIterator__(LinkType x = 0)
:_node(x)
{}
__ListIterator__(const Iterator& x)
{
_node = x._node;
}
bool operator==(const Iterator& x)
{
return _node == x._node;
}
bool operator!=(const Iterator& x)
{
return _node != x._node;
}
Reference operator*()
{
return (*_node)._data;
}
Pointer operator->()
{
return &(operator*());
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self operator++(int)
{
Self pTemp(*this);
_node = _node->_next;
return pTemp;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
Self pTemp(*this);
_node = _node->_prev;
return pTemp;
}
public:
LinkType _node;
};
template<class T>
class List
{
public:
typedef ListNode<T> Node;
typedef T ValueType;
typedef ValueType& Reference;
typedef const ValueType& ConstReference;
typedef ValueType* Pointer;
typedef const ValueType* ConstPointer;
typedef Node* LinkType;
typedef size_t SizeType;
typedef __ListIterator__<T, T&, T*> Iterator;
typedef __ListIterator__<T, const T&, const T*> ConstIterator;
public:
//构造函数
List()
{
EmptyInit();
}
List(SizeType n, const T& data)
{
EmptyInit();
for(size_t idx=0; idx<n; ++idx)
{
PushBack(data);
}
}
//拷贝构造函数
List(const List<T>& l)
{
EmptyInit();
Iterator it = l._node->_next;
for(SizeType idx=0; idx<l.Size(); ++idx)
{
PushBack(it._node->_data);
it++;
}
}
//析构函数
~List()
{
/*Iterator it = End();
--it;
while(it!=_node)
{
delete it._node;
--it;
}*/
Clear();
delete _node;
}
//赋值运算符重载
//List<T>& operator=(const List<T>& l)
//{
// List<T> pTempList(l);
// if(this != &l)
// {
// Iterator it = --End();
// while(it != _node)
// {
// it = Erase(it);
// }
// //Clear();//错误
// delete _node;
// _node = pTempList._node;
// }
// return *this;
//}
//
Iterator Begin()
{
return _node->_next;
}
ConstIterator Begin()const
{
return _node->_next;
}
Iterator End()
{
return _node;
}
ConstIterator End()const
{
return _node;
}
bool Empty()
{
return _node==_node->_next;
}
SizeType Size()const
{
Iterator it = _node->_next;
SizeType count = 0;
while(it != _node)
{
count = count+1;
it++;
}
return count;
}
SizeType MaxSize()const
{
return SizeType(-1);
}
Reference Front()
{
return *(Begin());
}
ConstReference Front()const
{
return *(Begin());
}
Reference Back()
{
return *(--End());
}
ConstReference Back()const
{
return *(--End());
}
Iterator Insert(Iterator pos, const T& x = T())
{
LinkType temp = new Node(x);
temp->_next = pos._node;
temp->_prev = pos._node->_prev;
pos._node->_prev->_next = temp;
pos._node->_prev = temp;
return temp;
}
void PushFront(const T& x)
{
Insert(Begin(),x);
}
void PushBack(const T& x)
{
Iterator it = End();
Insert(it,x);
}
//任意位置删除
Iterator Erase(Iterator pos)
{
Iterator pRemNode = pos._node->_next;
pos._node->_prev->_next= pos._node->_next;
pos._node->_next->_prev = pos._node->_prev;
pos._node->_next = pos._node->_prev = NULL;
delete pos ._node;
return pRemNode;
}
void PopFront()
{
Erase(Begin());
}
void PopBack()
{
Erase(--End());
}
void ReSize(SizeType n, const T& data)
{
if(Size() >= n)
{
SizeType size1 = Size()-n;
while(size1--)
{
PopBack();
}
}
else
{
SizeType size2 = n-Size();
while(size2--)
{
PushBack(data);
}
}
}
void Assign(SizeType n, const T& data)
{
Iterator pTemp = _node->_next;
if(Size()>=n)
{
for(size_t idx=0;idx<n; ++idx)
{
pTemp._node->_data = data;
++pTemp;
}
for(size_t idx = 0; idx<Size()-n; ++idx)
{
PopBack();
}
}
else
{
pTemp = _node->_next;
size_t size = Size();
for(size_t idx=0; idx<size; ++idx)
{
pTemp._node->_data = data;
++pTemp;
}
for(size_t idx = 0;idx<n-size; ++idx)
{
PushBack(data);
}
}
}
void Clear()
{
Iterator it = --End();
while(it != _node)
{
it = Erase(it);
}
_node->_next = _node;
_node->_prev = _node;
}
private:
void EmptyInit()
{
_node = new Node;
_node->_data = 0;
_node->_next = _node ;
_node->_prev = _node;
}
protected:
LinkType _node;
};