标签:
#ifndef SJTU_LIST_HPP
#define SJTU_LIST_HPP
#include <cstddef>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <climits>
#include <cstddef>
namespace sjtu {
class exception
{
protected:
const std::string variant = "";
std::string detail = "";
public:
exception() {}
exception(const exception &ec) : variant(ec.variant), detail(ec.detail) {}
virtual std::string what() {
return variant + " " + detail;
}
};
class index_out_of_bound : public exception {};
class runtime_error : public exception {};
class invalid_iterator : public exception {};
class container_is_empty : public exception {};
template<typename T>
class list {
private:
struct node
{
node* pre;
node* next;
T* data;
node()
{
next = NULL;
pre = NULL;
data = NULL;
}
~node()
{
delete data;
}
};
node* head;
node* tail;
int currentsize;
public:
list()
{
currentsize = 0;
head = new node;
tail = new node;
head->next = tail;
tail->pre = head;
head->pre = NULL;
tail->next = NULL;
}
list(const list<T> &other)
{
currentsize = 0;
head = new node;
tail = new node;
head->next = tail;
tail->pre = head;
head->pre = NULL;
tail->next = NULL;
node* tmp = head;
node* tmp2 = other.head;
for(int i = 1 ; i <= other.currentsize;i++)
{
tmp->next=new node;
tmp->next->pre=tmp;
tmp=tmp->next;
tmp2=tmp2->next;
tmp->data = new T(*(tmp2->data));
currentsize++;
}
tmp->next = tail;
tail->pre = tmp;
}
~list()
{
for(int i = currentsize;i>=1;i--)
{
node* p = tail->pre;
tail->pre=tail->pre->pre;
delete p;
}
delete head;
delete tail;
}
class iterator;
class const_iterator;
class iterator {
friend class list;
private:
node* p;
list<T>* thelist;
public:
iterator(const list<T>* ptrlist, node* ptrnode)
{
thelist = (list<T>*)ptrlist;
p=ptrnode;
}
iterator operator++(int)
{
iterator tmp = *this;
p=p->next;
return tmp;
}
iterator& operator++()
{
p=p->next;
return *this;
}
iterator operator--(int)
{
iterator tmp = *this;
p=p->pre;
return tmp;
}
iterator& operator--()
{
p=p->pre;
return *this;
}
T& operator*() const
{
return *(p->data);
}
bool operator==(const iterator &rhs) const
{
return p==rhs.p;
}
bool operator==(const const_iterator &rhs) const
{
return p==rhs.p;
}
bool operator!=(const iterator &rhs) const
{
return p != rhs.p;
}
bool operator!=(const const_iterator &rhs) const
{
return p != rhs.p;
}
T* operator->()
{
return p->data;
}
};
class const_iterator {
friend class list;
private:
node* p;
list<T>* thelist;
public:
const_iterator(const list<T>* ptrlist, node* ptrnode)
{
thelist = (list<T>*)ptrlist;
p = ptrnode;
}
const_iterator operator++(int)
{
iterator tmp = *this;
p=p->next;
return tmp;
}
const_iterator& operator++()
{
p=p->next;
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp = *this;
p=p->pre;
return tmp;
}
const_iterator& operator--()
{
p=p->pre;
return *this;
}
T& operator*() const
{
return *(p->data);
}
bool operator==(const iterator &rhs) const
{
return p==rhs.p;
}
bool operator==(const const_iterator &rhs) const
{
return p==rhs.p;
}
bool operator!=(const iterator &rhs) const
{
return p != rhs.p;
}
bool operator!=(const const_iterator &rhs) const
{
return p != rhs.p;
}
T* operator->()
{
return p->data;
}
};
void clear()
{
for (int i = currentsize;i >= 1;i--)
{
node* p = tail->pre;
tail->pre = tail->pre->pre;
delete p;
}
currentsize = 0;
}
list<T> & operator=(const list<T> &other)
{
if (&other == this) return *this;
clear();
node* p1 = head;
node* p2 = other.head->next;
for(int i = 1 ; i <= other.currentsize ; i ++)
{
p1->next = new node;
p1->next->pre = p1;
p1 = p1->next;
p1->data = new T(*(p2->data));
p2 = p2->next;
}
tail->pre = p1;
p1->next = tail;
currentsize = other.currentsize;
return *this;
}
const T & front() const
{
return *(head->next->data);
}
const T & back() const
{
return *(tail->pre->data);
}
iterator begin()
{
iterator tmp(this,head->next);
return tmp;
}
const_iterator cbegin() const
{
const_iterator tmp(this,head->next);
return tmp;
}
iterator end()
{
iterator tmp(this,tail);
return tmp;
}
const_iterator cend() const
{
const_iterator tmp(this,tail);
return tmp;
}
bool empty() const
{
return currentsize == 0;
}
size_t size() const
{
return (size_t)currentsize;
}
iterator insert(iterator pos, const T &value)
{
if (pos.thelist != this) throw invalid_iterator();
node* tmp = pos.getp();
tmp->pre->next = new node;
*(tmp->pre->next->data)=value;
tmp->pre->next->pre=tmp->pre;
tmp->pre->next->next = tmp;
tmp->pre = tmp->pre->next;
tmp=tmp->pre;
currentsize++;
return pos;
}
iterator erase(iterator pos)
{
if (pos.thelist != this) throw invalid_iterator();
currentsize--;
iterator tmp(this,pos.p->next);
node* p = pos.p;
if(p==head)
{
return tmp;
}
if(p==tail)
{
return tmp;
}
p->pre->next = p->next;
p->next->pre = p->pre;
delete p;
return tmp;
}
iterator erase(iterator first, iterator last)
{
if (first.thelist != this ||last.thelist != this)
throw invalid_iterator();
do
{
if (first.p == tail) throw invalid_iterator();
first=erase(first);
} while (first != last);
return first;
}
void push_back(const T &value)
{
node* p = new node;
p->data = new T(value);
p->pre = tail->pre;
tail->pre->next = p;
tail->pre = p;
p->next = tail;
currentsize++;
}
void pop_back()
{
node* p = tail->pre;
tail->pre=tail->pre->pre;
tail->pre->next = tail;
delete p;
currentsize--;
}
void push_front(const T &value)
{
node* p0 = head->next;
head->next = new node;
head->next->data = new T(value);
head->next->next = p0;
p0->pre = head->next;
head->next->pre = head;
currentsize++;
}
void pop_front()
{
node* p0 = head->next->next;
node* p1 = head->next;
delete p1;
head->next = p0;
p0->pre = head;
currentsize--;
}
};
}
#endif
标签:
原文地址:http://www.cnblogs.com/Chips/p/5405765.html