//自己造容器--List
/*
1、iterator
2、头迭代器
3、尾迭代器
4、链表长
5、判空
6、清除
7、取头元素
8、取尾元素
9、头插入
10、尾插入
11、头删除
12、尾删除
13、插入函数
14、删除函数
*/ template<typename Object>
class List
{
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){}
}; public:
//迭代器
class const_iterator
{
public:
const_iterator() :current(NULL){} //*重载
const Object & operator*()const
{
return retrieve();
} //pre++重载
const_iterator & operator++() //前置++,返回引用
{
current = current->next;
return *this; //this是一个指向iterator的指针
} //pre--
const_iterator & operator--() //前置--,返回引用
{
current = current->prev;
return *this; //this是一个指向iterator的指针
} //pos++重载
const_iterator operator++(int) //后置++,返回参数
{
const_iterator old = *this;
++(*this);
return old;
} //pos--
const_iterator operator--(int) //后置--,返回参数
{
const_iterator old = *this;
--(*this);
return old;
} //==重载
bool operator==(const const_iterator & rhs)const
{
return current == rhs.current;
} //!=重载
bool operator!=(const const_iterator & rhs)const
{
return !(current == rhs.current);
} protected: //在一般迭代器中变为private
Node*current; Object &retrieve()const
{
return current->data;
} const_iterator(Node*p) :current(p){} friend class List<Object>;
}; //一般迭代器继承常迭代器
class iterator : public const_iterator
{
public:
iterator(){} Object &operator*()
{
return retrieve();
} //以免被上一个operator*覆盖
const Object& operator*()const
{
return const_iterator::operator*();
} //pre++
iterator operator++()
{
current = current->next;
return *this;
} //pre--
iterator operator--()
{
current = current->prev;
return *this;
} //pos++
iterator operator++(int)
{
iterator old = *this;
++(*this);
return old;
} //pos--
iterator operator--(int)
{
iterator old = *this;
--(*this);
return old;
}
protected:
iterator(Node*p) : const_iterator(p){} friend class List<Object>;
}; public:
//the big three
List()
{
init();
} ~List()
{
clear();
delete head;
delete tail;
} List(const List &rhs)
{
init();
*this = rhs;
} const List& operator=(const List & rhs)
{
if (this == &rhs)
return *this;
clear();
for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr)
push_back(*itr);
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 size() == 0;
} //清除
void clear()
{
while (!empty())
{
pop_front();
}
} //取头元素
Object & front()
{
return *begin();
} const Object & front()const
{
return *begin();
} //取尾元素
Object & back()
{
return *--end();
} const Object & back()const
{
return *--end();
} //前插
void push_front(const Object&x)
{
insert(begin(), x);
} //后插
void push_back(const Object&x)
{
insert(end(), x);
} //前删
void pop_front()
{
erase(begin());
} //后删
void pop_back()
{
erase(--end());
} //插入
iterator insert(iterator itr, const Object &x)
{
theSize++;
Node *p=itr.current;
/*Node*q=new Node(x, p->prev, p);
p->prev->next=q;
p->prev=q;
return iterator(q);
*/
return iterator(p->prev = p->prev->next = new Node(x, p->prev, p));
} //单个删除
iterator erase(iterator itr)
{
Node*p = itr.current;
iterator retVal(p->next);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
theSize--; return retVal;
} //多个删除
iterator erase(iterator start, iterator end)
{
iterator itr;
for (itr = start; itr != end;)
itr = erase(itr);
return itr;
} private:
int theSize;
Node *head;
Node *tail; void init()
{
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
};