The internal relationship of STL's bidirectional circular list (list)

Source: Internet
Author: User
/* Simple list doubly linked list structure relationship * * Li Kun *326087275@qq.com * * * from simple to complex learning path */#include <stdio.h> template<typename t> CLA SS listnode//Observe the Linux source ListNode as the base class, T data is placed in its derived class, this example does not do its derived class {public:listnode<t>* prior;//point to the previous address ListNode <t>* next;//points to the next address T data;//does not do data derived classes, directly with the node class storage type data int nrecord;//whether the data has been recorded int nend;//end judgment, with variable flags, mainly to deal with the CString container


Without judging T data ListNode () {nrecord = 0; nend = 0;} virtual ~listnode () {}}; Template<typename t> class alloc//simple memory allocation {public:virtual ~alloc () {} listnode<t>* mallocmemory (listnode& Lt
t>* Obj,int & Capacity,bool Bclean = false);//The first parameter is the incoming linked list object, the second parameter represents the forward or backward increment of the node, whether the third parameter has been set at the end}; Template<typename t> inline listnode<t>* alloc<t>::mallocmemory (listnode<t>* Obj,int &
   Capacity,bool Bclean) {listnode<t> *_new_ = new listnode<t>;
   _new_->next = NULL;
_new_->prior = NULL; if (0 = = capacity)//Add node {if (!bclean) {while (1! = obj->nend) {obj = obj->next;} in order
obj->nend = 0;
} obj->next = _new_;//alignment container obj->next->prior = obj;
_new_->nend = 1;//end End flag} else//increment node {obj->prior = _new_; _new_->next = obj;} return _new_ in first place; } Template<typename t> class list_iterator//simple iterator {public:typedef list_iterator<t>
List_iter;
typedef listnode<t>* node;
typedef long int difference_type;
typedef t* Pointer;


typedef t& Reference; Explicit List_iterator (): Noperjia (1) {} pointer operator-> () const {return & (M_iternode->data);} reference O 
perator* () const {return m_iternode->data;} bool operator! = (List_iter & obj) {return (Noperjia! = Obj.noperjia); } int operator + + () {++noperjia; m_iternode = m_iternode->next; return 0;} int operator-() {--noperjia; M_iternod
E = m_iternode->prior;
return 0;
} node M_iternode;
int Noperjia;


};
Template<typename t> class Test {public:virtual ~test () {clean ();} typedef listnode<t>* node;typedef alloc<t> malloc;
typedef list_iterator<t> Iterator;
    Explicit Test (): Nbefore (0), Noperjia (1), Bclean (True) {Initnode ();
} int Initnode (); listnode<t>* allocmemory (int insert = 0,bool Bclean = false);//insert 0 is backward, 1 is forward, Bclean true first use does not calculate position. The list of multiple allocations is done at the end of the bool Push_back (const T & obj);//from the current backward insert bool Push_front (const T & obj);//Insert the bool Earse in the first position (iterator & Amp
obj);//The container is deleted, and the iterator is followed by the deletion of iterator Begin ();
Iterator End ();
BOOL Insert (iterator & obj,const T & _obj);//iteration increases with the addition of bool clean ();
Protected:node Lastnode;
Node Beforenode;
Node NextNode;
Node Endnode;
malloc Alloc_type;
int Nbefore;
int Noperjia;
Iterator Iter;
Private:bool bclean;//Determine if the data has been erased}; Template<typename t> Inline int test<t>::initnode () {listnode<t>* init = new listnode<t>; init-&
Gt;prior = NULL;
Init->next = NULL;
Lastnode = init;
Beforenode = init;
Endnode = init;
return 0; } template<typename t> inline listnode<t>* test<t>::allocmemory (int insert,bool bclean) {if (!insert) {Endnode = Alloc_type.mallocmemory (Beforenode,insert,bclean); Endnode
->next = Lastnode;
Lastnode->prior = Endnode;
This->bclean = false;
return endnode;
} lastnode = Alloc_type.mallocmemory (Lastnode,insert);
Lastnode->prior = Endnode;
Endnode->next = Lastnode;
return lastnode; } template<typename t> inline bool test<t>::p ush_back (const T & obj) {if (Bclean) {initnode (); Allocmemo
Ry (0,bclean);
Bclean = false;
  } else allocmemory (0);
 if (1 >= ++nbefore) {beforenode->data = obj;
 Beforenode->nrecord = 1;
 Lastnode = Beforenode;
 NextNode = beforenode->next;
  return true;
  } while (0! = Beforenode->nrecord) {Beforenode = beforenode->next;
  } beforenode->data = obj;
  Beforenode->nrecord = 1;
  NextNode = beforenode->next;
return true; } template<typename t> inline bool test<t>::p ush_front (const T & obj) {if (Bclean) {Initnode ();} allocm Emory (1); if (1 >= ++nbefore) {lastnode->data = obj; lastnode->nrecord = 1; beforenode = lastnode; nextnode = lastnode->
Next
return true;
while (1! = lastnode->prior->nend) {lastnode = lastnode->prior;} lastnode->data = obj;
Lastnode->nrecord = 1;
return true; } template<typename t> inline list_iterator<t> test<t>::begin () {iter.m_iternode = Lastnode; return it
Er } template<typename t> inline list_iterator<t> test<t>::end () {Iter.noperjia = (Nbefore + 1);//used to determine if
The last section iter.m_iternode = Beforenode;
return ITER; } template<typename t> inline bool Test<t>::insert (iterator & obj,const T & _obj)//Add or remove a section for both before and after The linked list establishes the up and down link {if (bclean) {initnode (); bclean = false;} if (1 = = Obj.noperjia)//The first node embeds the data for its own new space, without judging the space {node Savenode
= new listnode<t>;
Savenode->data = _obj;
Savenode->next = Lastnode;
Savenode->prior = Endnode;
Savenode->nrecord = 1;
Endnode->next = Savenode; Lastnode =Savenode;
Obj.m_iternode = Lastnode;
return true;
} node Addnode;
Addnode = new listnode<t>;
Addnode->data = _obj;
Obj.m_iternode->prior->next = Addnode;
Addnode->prior = obj.m_iternode->prior;
Obj.m_iternode->prior = Addnode;
Addnode->next = Obj.m_iternode;
Addnode->next->next = obj.m_iternode->next;
Obj.m_iternode = obj.m_iternode->prior;
Lastnode = Obj.m_iternode;
while (!lastnode->prior->nend)//Return to first bit lastnode = lastnode->prior;
return true; } template<typename t> inline bool Test<t>::earse (iterator & obj) {if (Bclean) {return false;} node get
Node,savenode,deletenode; if (1 = = Obj.noperjia) {if (!obj.m_iternode->nrecord) return false; GetNode = obj.m_iternode->next; Deletenode = OB
J.m_iternode;
Delete Deletenode;
Obj.m_iternode = GetNode;
Obj.m_iternode->prior = Endnode;
Obj.m_iternode->prior->next = Obj.m_iternode;
Lastnode = Obj.m_iternode; if (Obj.m_iternode->nend | | obj.m_iternode->next-&gT;nend) {endnode->next = Obj.m_iternode; obj.m_iternode->next = Endnode; return true;} return true;
} GetNode = obj.m_iternode->prior;
Deletenode = Obj.m_iternode;
Savenode = obj.m_iternode->next;
Delete Deletenode;
Obj.m_iternode = Savenode;
Obj.m_iternode->prior = GetNode;
Obj.m_iternode->prior->next = Obj.m_iternode;
Lastnode = Obj.m_iternode;
while (!lastnode->prior->nend) lastnode = lastnode->prior;
return true; } template<typename t> bool Test<t>::clean ()//The list is deleted, all nodes are empty {node deletenode; while (!lastnode-> Prior->nend)//forward check Delete {deletenode = lastnode->prior; Delete lastnode; lastnode = Deletenode;} while (!lastnode->
Nend)//1 for the end flag to remove {deletenode = lastnode->next; Delete lastnode; lastnode = deletenode;} delete lastnode;
Lastnode = NULL;
Beforenode = NULL;
NextNode = NULL;
Endnode = NULL;
Iter.m_iternode = NULL;
Nbefore = 0;
Noperjia = 0;
Bclean = true;
return true; } Template<typename t> class Testlist: Public test<t>//is just a variable name, no real use {public: ~testlist () {} testlist () {}}; 
/* Use instance testlist<int>test;//or test<int>test; for (int i = 0;i < 100;i++) {Test.push_back (1), Test.push_front (1);} for (Testlist<int>::iterator it = test. Begin (); It! = End (); it++) {*it;//invokes the reference operator, which displays incoming class data such as the value of it as 1.
If the passed-in type is a struct, you can output member data, or you can use the pointer operator it->data; Test.  Insert (it,1),///based on the current iteration to the link location of the data inserted, such as now is the 7th list, insert the 7th link list, the original 7th bit into the 8th bit test.earse (it,1);//delete the current link node, such as delete 7th bit, the original 8th bit into the 7th bit.


Insert and earse can be used throughout the iteration cycle, eliminating the problem of using Earse in STL to not call break.


The benefit of the}//iterator is that it avoids the loss of the list address due to the release of the iterator. Test.  Clear ();//Use the completion testlist call to release the linked memory.
You can also call the destructor automatically when the Testlist object is destroyed without calling this method, which is placed inside the destructor. */1. iterators according to my own realization, the iterator is used to query the data location of the linked list and update the linked list data.
In the example, the list node is added or deleted through an iterator, and the lists ' linked list object is refreshed, effectively preventing crashes caused by deleting nodes during the iteration.
2. The construction of the memory allocation doubly linked list has been done in memory allocation, and a section of the end is used to judge the data, effectively preventing such as CString container class bad judgment problem.
  3. The linked List method class establishes memory allocations, iterators, and links to linked list classes through this class, which is simple and flexible.

 Source file address http://download.csdn.net/detail/a29562268/9714680 for the first time to write a blog, there are many areas of understanding, please help to point out, thank you ~.

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.