c++ - Implementing begin() and end() for doubly linked list -


I wrote my container class whose original internal data structure was std :: list . Then I needed to make my double link list. I have now implemented my own linked list as well as my double linked list for my linked list, but I'm having trouble dealing with it as a std :: list , especially Start () and end () .

Which I think, must start () first point to node, and end () should denote the last element from the last element I need to make sure that when I call end () , then I can reduce the last element to the last I also need to make sure that I like my normal traversal I can ...

  while (beg! = End) {something; Beg; ++; }  

Essentially only the nodes are used in my linked list, in which the data element uses an indicator on the previous node and an indicator on the next node.

When I first tried to implement my end () , I had the next indicator of the last node a nullptr on its own Works but does not work in the same way as STL.

Any advice about how to get started () and end () What does the standard library do?

You can present a sensor node and a circular linked list.

A sketch:

  class list {private: structure node {node * previous; Node * next; Node (): previous (this), next (this) {}}; Structure DataNode: node {int data; }; Public: Class Iterator {List of Friends group; Private: Itater (node ​​* node): m_node (node) {} public: iterator (): m_node (0) {} iterator & amp; Operator ++ () {m_node = m_node-> next; Return * This; } Int'l & amp; Operator * () {// Note: Covering the end (central node) is undefined behavior. Return reinterpret_cast & lt; DataNode * & gt; (M_node) - & gt; Data; } // More Iterator Function Private: node * m_node; }; Public: List (): M_ Sentinel (node ​​node) {} Iterator start () {Return iterator (m_sentinal-> Next); } Iterator End () {Return Iterator (M_ Sentinel); } Itater Dot (iterator status, int data) {DataNode * data_node = new DataNode; // Pass data node * current = position. M_node; Data_node-> Next = present; Data_ode-> Previous = Current-> Previous; Current-> Previous-> Next = data_node; Current-gt; Previous = data_node; Return Iterator (Current); } Nil attachment (int data) {insert (end (), data); } Private: node * m_sentinal; };  

Comments