<span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace std;//does not have the basic functionality of a doubly linked list implemented with iterators and spatial Configurator Template<class _ty> Define the Template classes Class list//list class {public:typedef size_t size_type; Type redefinition protected:struct _node; Structural body _nodefriend struct _node; friend typedef _node* _NODEPTR; Type redefined struct _node//struct definition {_nodeptr _next,_prev;_ty _value;}; Protected:_nodeptr _head; Size_type _size;public:_nodeptr _buynode (_nodeptr _narg = 0, _nodeptr _parg = 0)//purchase Node {_nodeptr _s = (_nodeptr) malloc (Size Of (_node)); _s->_next = _narg! = 0? _narg: _s; _s->_prev = _parg! = 0? _parg: _s;return (_s); }public:size_type size () const//Length {return (_size);} BOOL Empty () const//sentencedEmpty {return (size () = = 0);} Explicit list (): _head (_buynode ()), _size (0)//list parameterless constructor {}explicit list (size_type _n, const _ty& _v): _hea D (_buynode ()), _size (0)//list constructor {Insert (_n, _v);} _nodeptr begin ()//First node {return _head->_next;} _nodeptr end ()//head node {return _head;} void Insert (_nodeptr _p,const _ty& _x)//Insert Node {_nodeptr _s = _p;_s->_prev = _buynode (_s,_s->_pr EV); _s = _s->_prev;_s->_prev->_next = _s;_s->_value = _x;++_size;} void Insert (Size_type _m, const _ty& _x)//Insert _m _x node {for (; 0 < _m;--_m) {insert (Begin (), _x);}} void Push_front (const _ty& _x)//head interpolation {insert (Begin (), _x);} void Pop_front ()//Header Delete {erase (begin ());} void push_back (const _ty& _x)//tail plug {Insert (end (), _x);} void Pop_back ()//tail Delete {erase (enD ()->_prev);} void Assign (Size_type _n, const _ty& _x)//Reinsert {clear (); Insert (_n, _x);} _nodeptr Erase (_nodeptr _p)//delete Node {_nodeptr _s = _p++;_s->_prev->_next = _s->_next;_ S->_next->_prev = _s->_prev;free (_s);--_size;return (_p);} void clear ()//clear {_nodeptr _p = _head->_next;while (_p! = _head) {_head->_ Next = _p->_next;_p->_next->_prev = _p->_prev;free (_p); _p = _head->_next; }_head->_next = _head->_prev;_size = 0;} void Show ()//print {_nodeptr _p = _head->_next;while (_p! = _head) {COUT<&L t;_p->_value<< "--"; _p = _p->_next;} cout<< "Over" <<ENDL;} ~list ()//destructor {clear (); free (_head); _head = 0, _size = 0;}}; void Main () {list<int> mylist (5,1); Mylist.show (); Mylist.insert (2,4); Mylist.show (); Mylist.push_front (3); Mylist.show (); MYLIST.PUsh_back (4); Mylist.show (); Mylist.pop_front (); Mylist.show (); Mylist.pop_back (); Mylist.show (); Mylist.clear (); Mylist.show (); mylist.assign (2,3); Mylist.show ();} </span>
This implementation is still a lot of problems have not been resolved, in the later stage will be followed up to improve, but also hope that we point out the wrong suggestions, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implementation of the "C++/stl" list (without the basic functionality of a doubly linked list implemented with iterators and spatial configurator)