Four STL Anatomy--list

Source: Internet
Author: User

List.h

#ifndef  __List__#define __List__#include  "Iterator.h" #include   "Alloc.h" #include   " Construct.h "//stl for the node of the//list after the interval front closed template<class t>struct listnode{typedef listnode<t >* Node_p; node_p _prev; node_p _next; t _data; ListNode (Const t& data=t ()  ): _prev (null),  _next (null), iterator in  _data (data) {}};//list (Because the list is not a linear space structure, the generic iterator does not move properly, so the list needs to define a specialized iterator.) ) template<class t,class ref,class ptr>//??????????????? The iterator used by struct __listiterator{//list should be bidirectional (five corresponding types must have, or inherit Struct iterator) typedef  bidirectionaliteratortag iteratorcategory;typedef t valuetype;typedef ptr pointer; Typedef ref reference;typedef ptrdiff_t differencetype;typedef __listiterator<t,  Ref, Ptr> Self;typedef ListNode<T> Node, *Node_p; Node_p _node;//__listiterator (node_p x)  //: _node (x)  //{}__liStiterator () {}__listiterator (Node_p node): _node (node) {}//The value of the node ref operator* () const{return _ Node->_data;} Ptr operator-> () const{return & (operator* ());} bool operator ==  (const self& x) {return  (_node==x._node);} bool operator !=  (const self& x) {return  (_node != x._node);} self& operator++ () {_node = _node->_next;return *this;} self operator++ (int) {self old = *this;++ (*this); return old;} self& operator--() {(_node = _node->_prev); return *this;} self operator--(int) {self old = *this;--(*this); return old;}};/ /template <class t, class ref, class ptr>// bidirectionaliteratortag  iteratorcategory (const __listiterator<t, ref, ptr>&)  //{//return  Bidirectionaliteratortag ();//}//template <class t,&nbsP;class ref, class ptr>//t* valuetype (const __listiterator<t, ref,  ptr>&)  //{//return 0;//}//template <class t, class ref, class  ptr>//ptrdiff_t* distancetype (const __listiterator<t, ref, ptr>&)   {//return 0;//}//list is a two-way loop linked list of leading nodes Template<class t, class alloc = alloc >class List{public:typedef SimpleAlloc<ListNode<T>, Alloc>  LISTNODEALLOCATOR;TYPEDEF&NBSP;__LISTITERATOR&LT;T,&NBSP;T&AMP;,&NBSP;T*&GT;&NBSP;ITERATOR;//STL mandatory typedef  __listiterator<t, const t&, const t*> constiterator;typedef t  valuetype;typedef t& reference;typedef const t& constreference;typedef  T* Pointer;typedef const T* ConstPointer;//typedef List<T, Alloc>  self;private:typedef listnode<t> Node, *Node_p; Node* createnode (Size_t size,const t& data=t ()) {//node_p node = new  node (data);//Allocate memory node* node = listnodeallocator::allocate (size);//config space construct (Node,  data); return node;} Void destroynode (Node_p node) {//Call the destructor Destroy (node);//Release Memory Listnodeallocator::D eallocate (node);} Public:list (): _list (CreateNode (sizeof (Node))) {_list->_next = _list;_list->_prev = _ List;} List (Iterator first,iterator last);//list (const list<t, alloc>& l);//List <t,alloc>& operator= (const list<t,alloc>& l); ~List () {Clear ();//DestroyNode ( Node_p node)//destorynode (_list); _list = null;} /*~list () {size_t size = size ();D Estroy (Begin (), End ()); Listnodeallocator::D eallocate (_list,size);} */iterator begin ()  {return _list->_next;} Iterator end ()  {return _list;} Constiterator begin ()  const{return _list->_next;//return constiterator (_list->_next);} Constiterator end ()  const{return _list->_next;//return constiterator (_list);} Void pushback (const t& data) {Insert (End (), data);} Void popback () {iterator tmp = end (); Erase (--tmp);} Void pushfront (const t& data) {Insert (Begin (), data);} Void popfront () {Erase (Begin ());} The contents of the header node (element value) Reference front () {Return *begin ();} Constreference front () Const{}reference back () {return * (--end ());} Constreference back () const{}//insert Void insert in front of the current position (iterator pos, const t&  Data) {node_p cur = pos._node; Node_p prev = cur->_prev;//node_p tmp = new node (data); Node_p tmp = createnode (sizeof (Node), data); tmp->_next = cur;cur->_prev =  tmp;prev->_next = tmp;tmp->_prev =&Nbsp;prev;} Iterator erase (Iterator pos) {node_p cur = pos._node; node_p prev = cur->_prev; node_p next = cur->_next;prev->_next = next;next->_prev = prev; Delete cur;return iterator (next);} Bool empty () const{if  (_list->_next == _list) {return true;} Else{return false;}} Size_t size () {/*size_t size;distance (Begin (), End (), Size) return size;*/int size =  0; node_p start = _list->_next; node_p end = _list;while  (start != end) {++size;start = start->_next ;} Return size;} Void clear ()//all the elements in the list container are dropped{ Iterator tmp = begin ();while  (Tmp != end ()) {//destroynode (*tmp);//error// Destroynode (tmp._node); ++tmp;} _list->_prev = _list;_list->_next = _list;} TheThe IGN function is used to reset the contents of the list//void assign (inputiterator first, inputiterator last);//To reset the//void with a contiguous space  assign (size_type n, const t& u);//Reset the//void remove with a specific value (const T& &NBSP;X);//void sort ();//void unique ();//void reverse ();//void merge (List<T,Alloc> &AMP;&NBSP;L);//The Join function, which joins the following into the position specified by POS//A list of all values//A list of iterators specified by the value//the value that is pointed to by the iterator specified by a list of two iterators//void splice ( ITERATOR&NBSP;POS,LIST&LT;T,ALLOC&GT;&AMP;&NBSP;X);//void splice (iterator pos, list<t,  Alloc>& x,iterator it);//void splice (iterator pos, list<t, alloc> & x,iterator first,iterator last);//void swap (list<t,alloc>& l);// Random access not supported, no [] overloaded protected:node_p _list;}; #endif

Test.cpp

Void printlist1 (list<int> l) {list<int>::iterator ite = l.begin (); *ite  = 2;for  (Ite = l.begin ();  ite != l.end ();  ++ite) {std::cout  << *ite << std::endl;}} Void printlist2 (const list<int> l) {list<int>::constiterator ite = l. Begin ();//*ite = 2;for  (Ite = l.begin ();  ite != l.end ();  ++ite) { Std::cout << *ite << std::endl;}} Void testlist () {/*std::list<aa> l1;std::list<aa>::iterator items;l1.push_back (AA ()) L1.push_back (AA (1,  ' B ')), L1.push_back (AA (2,  ' C ')), L1.push_back (AA (3,  ' d '));items =  L1.begin ();for  (;  items != l1.end (); ) {std::cout << items->_a  <<  "  "  <<items->_c <<std::endl;items++;} */list<int> l;l.pushbaCK (1); L.pushback (2); L.pushback (3); L.pushback (4); list<int>::iterator ite;for  (Ite = l.begin ();  ite != l.end ();  ++ ITE) {Std::cout << *ite << std::endl;} Std::cout << l.size ()  << std::endl;ite = l.begin (); L.Insert (ite,100) ; Std::cout << *ite << std::endl;l.popfront (); L.popback (); L.PushFront (%); L. Pushfront (100); List<int>::iterator it = l.begin (); List<int>::iterator its = l.end (); list<int>::iterator item;std::cout<<*it<<std::endl;++it;its--;std::cout <<   (it != its)  << std::endl;int data1 = l.front (); int data2=  l.back (); Std::cout<<l.size () <<std::endl;std::cout << !l.empty ()  < < std::endl;//list<int> l;//l.pushback (1);//l.pushback (2);////printlist1 (l);//printlist2 (l);} 


This article from "Zero Egg" blog, declined reprint!

Four STL Anatomy--list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.