#ifndef OPP_LIST #define OPP_LIST namespace opp { template class listhead { public: listnode * succ, * pred; listhead() { succ = (listnode *)this; pred = (listnode *)this; } }; template class listnode : public listhead { public: T data; listnode(const T & t) : data(t) {} listnode(T && t) : data(t) {} }; template class list_iterator { public: listnode * node; public: list_iterator(void) : node(nullptr) {} list_iterator(listnode * n) : node(n) {} list_iterator(const list_iterator & li) : node(li.node) {} list_iterator & operator=(const list_iterator & li) { node = li.node; return *this; } T & operator*() { return node->data; } T * operator->() { return &(node->data); } list_iterator & operator++(void) { node = node->succ; return *this; } list_iterator operator++(int) { listnode * n = node; node = node->succ; return list_iterator(n); } list_iterator & operator+=(int n) { while (n--) node = node->succ; return *this; } list_iterator & operator--(void) { node = node->pred; return *this; } list_iterator operator++(int) { listnode * n = node; node = node->pred; return list_iterator(n); } list_iterator & operator-=(int n) { while (n--) node = node->pred; return *this; } bool operator==(const list_iterator & li) { return node == li.node; } bool operator!=(const list_iterator & li) { return node != li.node; } }; template class list { private: typedef listnode ln; listhead head; public: typedef T element_type; typedef list_iterator iterator_type; list(void) {} ~list(void) { listnode * n = head.succ; while (n != &head) { listnode * m = n->succ; delete n; n = m; } } list_iterator begin(void) { return list_iterator(head.succ); } list_iterator end(void) { return list_iterator((listnode *)&head); } T & front(void) { return head.succ->data; } const T & front(void) const { return head.succ->data; } T & back(void) { return head.pred->data; } const T & back(void) const { return head.pred->data; } list_iterator erase(list_iterator it); list_iterator erase(list_iterator first, list_iterator last); void pop_front(void); void pop_back(void); void push_front(const T & t); void push_front(T && t); void push_back(const T & t); void push_back(T && t); list_iterator insert(list_iterator it, const T & t); list_iterator insert(list_iterator it, T && t); }; template void list::pop_front(void) { listnode * n = head.succ; head.succ = n->succ; n->succ->pred = (listnode *)&head; delete n; } template void list::pop_back(void) { listnode * n = head.pred; head.pred = n->pred; n->pred->succ = (listnode *)&head; delete n; } template void list::push_front(const T & t) { listnode * n = new listnode(t); n->pred = (listnode *)&head; n->succ = head.succ; head.succ->pred = n; head.succ = n; } template void list::push_front(T && t) { listnode * n = new listnode(t); n->pred = (listnode *)&head; n->succ = head.succ; head.succ->pred = n; head.succ = n; } template void list::push_back(const T & t) { listnode * n = new listnode(t); n->succ = (listnode *)&head; n->pred = head.pred; head.pred->succ = n; head.pred = n; } template void list::push_back(T && t) { listnode * n = new listnode(t); n->succ = (listnode *)&head; n->pred = head.pred; head.pred->succ = n; head.pred = n; } template list_iterator list::erase(list_iterator it) { listnode * n = it.node; listnode * s = n->succ; n->succ->pred = n->pred; n->pred->succ = n->succ; delete n; return list_iterator(s); } template list_iterator list::erase(list_iterator first, list_iterator last) { listnode * n = first.node; listnode * s = last.node; n->pred->succ = s; s->pred = n->pred; while (n != s) { listnode * m = n->succ; delete n; n = m; } return list_iterator(s); } template list_iterator list::insert(list_iterator it, const T & t) { listnode * n = new listnode(t); n->succ = it.node; n->pred = it.node->pred; it.node->pred->succ = n; it.node->pred = n; return list_iterator(n); } template list_iterator list::insert(list_iterator it, T && t) { listnode * n = new listnode(t); n->succ = it.node; n->pred = it.node->pred; it.node->pred->succ = n; it.node->pred = n; return list_iterator(n); } } #endif