This time the code is longer, because it is more than the previous vector implementation of a lot of operations, and after all, the pointer operation, it is a little more cumbersome to handle.
A very important point in the list implementation is to define a head pointer and a tail pointer, so as to avoid the processing of many special cases, when the list is empty is the head pointer pointing to the tail pointer, the other is the implementation of the iterator,
The implementation of the list iterator is much more cumbersome than the vector, because the memory is not contiguous, and all of the internal nested classes are overloaded with * (dereference), ++exp (front + +), ++exp (post + +), = = and! = Equal operator.
Specific people can see the code.
<span style= "FONT-SIZE:14PX;" >//list Template<typename T>class list{private://define node and set initialization function for a node struct node{t T; node* Pre; Node* Next; Node (t tval=t (), node* preval=null,node* nextval=null): t (tval), Pre (PreVal), Next (Nextval) {}};p ublic:/* The implementation of Const iterators and non-const iterators, because the const iterators are only as protection for data, many operations are the same, so here is the inheritance */class const_iterator{public:const_iterator (): Current (NULL) {}const t& operator* () Const{return retrieve ();} Overloads the associated operator const_iterator& operator++ () {Current=current->next;return *this;} Const_iterator operator++ (int) {const_iterator old=*this;++ (*this); return old;} BOOL operator== (const_iterator& RHS) Const{return current==rhs.current;} BOOL Operator!= (const_iterator& RHS) Const{return current!=rhs.current;} protected:node* current; t& Retrieve () Const{return current->t;} Const_iterator (node* P): current (p) {}friend class list<t>;}; Class Iterator:public Const_iterator{public:iterator (): Current (NULL) {}t& operator* () {return retrieve ();} iterator& operator++ () {CURrent=current->next;return *this;} Iterator operator++ (int) {iterator old=*this;current=current->next;return old;} Const t& operator* () Const{return const_iterator::operator* ();} Protected:iterator (node* P): Const_iterator (p) {}friend class list<t>;}; Public:/* constructor and destructor, the Init function belongs to the private member, so it is placed below */list () {init ();} List (const list& RHS) {init (); operator= (RHS);} ~list () {clear ();d elete head;delete Tail;} The overloaded operator Const list& operator= (const list& RHS) {for (const_iterator cite=rhs.begin (); Cite!=rhs.end (); cite++) { Push_back (*cite);} return *this;} t& operator[] (int index) {iterator ite=begin (); for (int i=0;i<index;i++) Ite++;return *ite;} The associated operation function t& Front () {return *begin ();} Const t& Front () Const{return *begin ();} t& back () {return * (--end ());} Const t& Back () Const{return * (--end ());} void Push_front (T t) {node* p=new Node (t);p->next=head->next;p->pre=head;p->next->pre=p;head-> next=p;thesize++;} void Push_back (T t) {node* p=new Node (t);p->PRE=TAIL-≫pre;p->next=tail;p->pre->next=p;tail->pre=p;thesize++;} void Pop_front () {node* p=head->next;head->next=p->next;p->next->pre=head;delete p;theSize--;} void Pop_back () {node* p=tail->pre;tail->pre=p->pre;p->pre->next=tail;delete p;theSize--;} void Insert (iterator itr,t T) {node* p=new Node (t);p->next=itr.current;p->pre=itr.current->pre;itr.current- >pre->next=p;itr.current->pre=p;thesize++;} Iterator Erase (iterator itr) {node* p=itr.current->next;p->pre=itr.current->pre;p->pre->next=p; Delete Itr.current;return iterator (p); thesize--;} void Erase (iterator Start,iterator end) {for (iterator ite=start;ite!=end) {ite=erase (ite); thesize--;}} Iterator begin () {return iterator (head->next);} Const_iterator begin () Const{return const_iterator (Head->next); Iterator End () {return iterator (tail);} Const_iterator End () Const{return const_iterator (tail);} int size () Const{return thesize;} bool Empty () Const{return thesize==0;} void Clear () {WHIle (thesize!=0) {pop_back ();}} Private:int thesize; node* Head; node* tail;void init () {thesize=0;head=new node (); tail=new node ();head->next=tail;tail->pre=head;}}; </span>
If you have any questions or errors, please leave a message.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + list bidirectional linked list implementation will also be written