The following code is only used by me to review the data structure. The practicality of N is low ~~ Haha:>
/// Example of a simple two-way linked list using the C ++ template technology. // # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // pre-declaration template <typename T> class doublylinkedlist; //// bidirectional linked list node template. // template <typename T> class node {friend class doublyshortlist <t>; private: T _ value; node <t> * _ pprior, * _ pnext; public: node (void): _ pprior (null), _ pnext (null) {NULL;} explicit node (const T & Val): _ value (VAL), _ pprior (null), _ Pnext (null) {NULL;} T & getvalue (void) {return _ value;} node <t> * getprior (void) {return _ pprior ;} node <t> * getnext (void) {return _ pnext ;}}; // bidirectional linked list class template // template <typename T> class doublylinkedlist {PRIVATE: node <t> * _ phead; public: doublyshortlist (void );~ Doublyshortlist (void); void clear (void); size_t length (void) const; T & visit (const size_t POS); node <t> * Add (const T & Val ); node <t> * insert (const size_t POs, const T & Val); node <t> * search (const T & Val) const; node <t> * remove (const size_t POS) ;}; template <typename T> doublyshortlist <t >:: doublyshortlist (void) {_ phead = new node <t> ();} template <typename T> doublyappslist <t> ::~ Doublyshortlist (void) {clear (); Delete _ phead;} // clear all the nodes inserted in the linked list, except for the header nodes. // template <typename T> void doublyasklist <t>: clear (void) {for (node <t> * pdel = _ phead-> _ pnext; null! = Pdel; pdel = _ phead-> _ pnext) {_ phead-> _ pnext = pdel-> _ pnext; # If! Defined (ndebug) STD: cout <"delete value:" <pdel-> _ value <STD: Endl; # endifdelete pdel ;}} //// calculate the total number of nodes and exclude the header node. // template <typename T> size_t doubly1_list <t>: length (void) const {size_t Len = 0; for (node <t> * ptemp = _ phead-> _ pnext; null! = Ptemp; ++ Len) {ptemp = ptemp-> _ pnext;} return Len;} // insert a node to the POS at the specified position. If the POS is too large, insert at the end of the table. returns the inserted node pointer. // template <typename T> T & doublyshortlist <t>: visit (const size_t POS) {node <t> * pvisit = _ phead-> _ pnext; for (size_t I = 0; null! = Pvisit & I <Pos; ++ I, pvisit = pvisit-> _ pnext) {null ;}if (null = pvisit) {Throw STD :: overflow_error ("accessing linked list nodes out of bounds! ");} Return pvisit-> _ value;} // Insert a new node after the linked list header node. // template <typename T> node <t> * doublyshortlist <t>: add (const T & Val) {node <t> * pnew = new node <t> (VAL); pnew-> _ pnext = _ phead-> _ pnext; _ phead-> _ pnext = pnew; return pnew;} // insert a node at the specified position. If the POS is too large, insert the node at the end of the table. returns the inserted node pointer. // template <typename T> node <t> * doubly1_list <t>: insert (const size_t POs, const T & Val) {node <t> * pprev = _ phead; for (size_t I = 0; n Ull! = Pprev-> _ pnext & I <Pos; ++ I) {pprev = pprev-> _ pnext ;} node <t> * pnew = new node <t> (VAL); pnew-> _ pnext = pprev-> _ pnext; pprev-> _ pnext = pnew; # If! Defined (ndebug) STD: cout <"insert node:" <pnew-> _ value <STD: Endl; # endifreturn pnew ;} //// find the node based on the specified data value and find the return node pointer. Otherwise, null is returned. // template <typename T> node <t> * doublyshortlist <t>: Search (const T & Val) const {node <t> * ptemp = _ phead-> _ pnext; For (null; null! = Ptemp & Val! = Ptemp-> _ value; ptemp = ptemp-> _ pnext) {NULL;} return ptemp;} // Delete the specified node and return the pointer to the previous node, // If the POs value is too large, the last node pointer is returned. // template <typename T> node <t> * doublyshortlist <t>: Remove (const size_t POS) {node <t> * pprev = _ phead; for (size_t I = 0; null! = Pprev-> _ pnext & I <Pos; ++ I) {pprev = pprev-> _ pnext;} If (null! = Pprev-> _ pnext) {node <t> * pdel = pprev-> _ pnext; pprev-> _ pnext = pdel-> _ pnext; # If! Defined (ndebug) STD: cout <"delete node:" <pdel-> _ value <STD: Endl; # endifdelete pdel;} return pprev ;} /// call after the main function is completed to prevent the console from passing through. // void calledaftermain (void) {system ("pause") ;}/// test the linked list // int main (void) {const size_t max_list_size = 10; doublyshortlist <int> shortlist; For (size_t I = 0; I <max_list_size; ++ I) {// shortlist. insert (I, I); into list. add (I);} STD: cout <"chain table length:" <shortlist. length () <STD: Endl; try {for (size_t I = 0; I <max_list_size + 1; ++ I) {STD: cout <STD :: SETW (3) <shortlist. visit (I);} STD: cout <STD: Endl;} catch (const STD: exception & E) {STD: cout <STD :: endl <E. what () <STD: Endl;} atexit (calledaftermain); Return exit_success ;}