# Include <iostream> // STL dual-chain table, which works with the iterator # include "mymemory. H "# include" myconstruct. H "template <typename T> struct _ list_node {typedef void * void_pointer; void_pointer Prev; // it can be set to _ list_node <t> * void_pointer next; t data ;}; template <typename T, typename ref, typename PTR> struct _ list_iterator {// What are the functions of this template? Typedef _ list_iterator <T, T &, T *> iterator; typedef _ list_iterator <t, ref, PTR> self; typedef extends iterator_category; typedef t value_type; typedef PTR pointer; typedef ref reference; typedef _ list_node <t> * link_type; typedef size_t size_type; typedef ptrdiff_t difference_type; link_type node; // This is the link between the iterator and the container // constructor _ list_iterator (link_type X): node (x) {}__ list_iterator () {}__ list_it Erator (const iterator & X): node (X. node) {}/* The implementation here may be a little poor. * According to C ++ primer's suggestion, the following four principles are used to reload operators: * 1. =, [], (),-> must be a class member * 2. the operator of compound assignment is usually defined as a class member * 3. changing the object status or closely related to a given type should usually be defined as a class member, such as auto-increment, auto-subtraction, and unreference * 4. symmetric operators, such as arithmetic, equal, relational, and bit operators, are best defined as common member functions */bool operator = (const self & X) const {return node = x. node;} bool Operator! = (Const self & X) const {return node! = X. node;} reference operator * () const {return node-> data;} // value: pointer operator-> () const {return & (operator *());} // note self & operator ++ () {node = (link_type) (node-> next); return * This;} self operator ++ (INT) {self TMP = * This; ++ * This; return TMP;} self & operator -- () {node = (link_type) (node-> PREV); return * This ;} self operator -- (INT) {self TMP = * This; -- * This; return TMP ;}}; template <typename T, typename alloc = alloc> class List {public: typedef t value_type; typedef value_type * pointer; typedef const value_type * pointer; typedef value_type & reference; typedef const value_type & const_reference; typedef size_t size_type; typedef Comment comment; typedef _ list_iterator <T, T &, T *> iterator; protected: typedef _ list_node <t> list_node; typedef simple_alloc <list_node, alloc> node_allocator; public: typedef list_n Ode * link_type; protected: link_type node; // a pointer that cyclically traverses the entire circular two-way linked list as long as it points to a blank node at the end, // The Node changes to be able to meet the requirements of the forward, backward, and closed, and become the last iterator. The next node points to the head node of the linked list. If the linked list // is empty, it points to its own public: List () {empty_initialize ();} // generates an empty linked list iterator begin () {return (link_type) (node-> next);} iterator end () {return node ;} bool empty () const {return node-> next = node;} inline size_t size () const; reference Front () {return * begin ();} reference back () {return * (-- end ());}/ /Insert a node before position. The content is xiterator insert (iterator position, const T & X); void push_back (const T & X) {insert (end (), x);} void push_front (const T & X) {insert (begin (), x);} iterator erase (iterator position ); // remove the node void pop_front () {erase (begin ();} void pop_back () {erase (-- end ();} void clear (); // clear the linked table void remove (const T & value); // remove the element void unique () with the same value as the value; // remove the continuous element public with the same value: // exchange two linked lists; void swap (List & X) {L Ink_type TMP; TMP = node; node = x. node; X. node = TMP;} // move all elements in [first, last) to void transfer (iterator position, iterator first, iterator last) before position; void splice (iterator position, list & X) {// concatenates X before position, and X is different from * thisif (! X. empty () Transfer (Position, X. begin (), X. end ();} void splice (iterator position, list &, iterator I); // Concatenates the elements I refer to before position. // [first, last) void splice (iterator position, list &, iterator first, iterator last) {If (first! = Last) Transfer (position, first, last);} void merge (List & X); // both lists are sorted in ascending order, and X is merged into * thisvoid reverse (); // flip void sort (); // here we use the merge sort protected: // configure a node and return link_type get_node () {return node_allocator: allocate ();} // release a node void put_node (link_type p) {node_allocator: deallocate (p) ;}// generate (configure and construct) a node, element value link_type create_node (const T & X) {link_type P = get_node (); construct (& (p-> data), x); Return P ;} // destroy (analyze and release) a node void Destroy_node (link_type p) {destroy (& (p-> data); put_node (p) ;}inline void empty_initialize () ;}; // define template <typename t, typename alloc> inline size_t list <t, alloc >:: size () const {size_t result = 0; link_type P = node-> next; while (P! = Node) {++ result; P = p-> next;} return result;} // your template <typename T, typename alloc> inline void list <t, alloc>:: empty_initialize () {node = get_node (); node-> next = node; node-> Prev = node;} // your template <typename t, typename alloc> typename list <t, alloc >:: iterator list <t, alloc >:: insert (Iterator position, const T & X) {link_type TMP = create_node (x); TMP-> next = position. node; TMP-> Prev = position. node-> Prev; (link_type (position. node-> PREV)-> next = TMP; position. node-> Prev = TMP; return TMP;} // your template <typename T, typename alloc> typename list <t, alloc >:: iterator list <t, alloc> :: erase (iterator position) {link_type next_node = link_typ E (position. node-> next); link_type prev_node = link_type (position. node-> PREV); prev_node-> next = next_node; next_node-> Prev = prev_node; destroy_node (position. node); Return iterator (next_node);} // your template <typename T, typename alloc> void list <t, alloc >:: clear () {link_type cur = (link_type) node-> next; while (cur! = Node) {link_type TMP = cur; cur = (link_type) cur-> next; destroy (TMP);} node-> next = node; node-> Prev = node ;} // define template <typename T, typename alloc> void list <t, alloc >:: remove (const T & Value) {iterator first = begin (); iterator last = end (); While (first! = Last) {iterator next = first; ++ next; If (* First = value) Erase (first); first = next ;}// your template <typename t, typename alloc> void list <t, alloc >:: unique () {iterator first = begin (); iterator last = end (); If (first = last) return; iterator next = first; while (++ next! = Last) {If (* First = * Next) Erase (next); elsefirst = next; next = first ;}// your template <typename t, typename alloc> void list <t, alloc>: Transfer (iterator position, iterator first, iterator last) {If (position! = Last) {(* (link_type (* last. node ). PREV ))). next = position. node; (* (link_type (* first. node ). PREV ))). next = last. node; (* (link_type (* position. node ). PREV ))). next = first. node; link_type TMP = link_type (* position. node ). PREV); (* last. node ). prev = (* first. node ). prev; (* first. node ). prev = TMP ;}// --------------------------------------------------------------------------------- template <typename T, typename alloc> void list <t, allo C >:: splice (iterator position, list &, iterator I) {iterator J = I; ++ J; If (position = I | position = J) return; transfer (Position, I, j);} // your template <typename T, typename alloc> void list <t, alloc>: Merge (list <t, alloc> & X) {iterator first1 = begin (); iterator last1 = end (); iterator first2 = x. begin (); iterator last2 = x. end (); While (first1! = Last1 & first2! = Last2) {If (* first2 <* first1) {iterator next = first2; Transfer (first1, first2, ++ next);} else ++ first1;} If (first2! = Last2) Transfer (last1, first2, last2);} // your template <typename T, typename alloc> void list <t, alloc >:: reverse () {// empty linked list or if (node-> next = node | link_type (node-> next)-> next = node) return; iterator first = begin (); ++ first; while (first! = End () {iterator old = first; ++ first; Transfer (begin (), old, first );}} // success // template <typename T, typename alloc> void list <t, alloc>: Sort () // here mergesort is not as good as quicksort, however, the implementation method here is interesting. // It is worth noting that {If (node-> next = node | link_type (node-> next)-> next = node) return; // list of intermediary data storage areas <t, alloc> carry; List <t, alloc> counter [64]; int fill = 0; // counter [0] Stores 2 elements, count Er [1] stores the four elements of sorting ..... While (! Empty () {// each time this cycle starts, carry retrieves an element in the original list, carry. splice (carry. begin (), * This, begin (); // if there is an element in counter [0], merge carry with counter [0] first, if counter [0] // is null, the elements in carry are given to counter [0], and then the merged elements are passed to carry, counter [0] is null // If counter [1] already contains two elements, merge them. If not, pass the two elements to counter [1] // and then carry and then take the elements .... Repeating int I = 0; while (I <fill &&! Counter [I]. empty () {counter [I]. merge (carry); Carry. swap (counter [I]); ++ I;} carry. swap (counter [I]); if (I = fill) ++ fill;} For (INT I = 1; I <fill; ++ I) {counter [I]. merge (counter [I-1]);} swap (counter [fill-1]);}