The following code is only used by me to review the data structure. The practicality of N is low ~~ Haha:>
//// Example of a simple linked list using the C ++ template technology. // # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // template pre-declaration for the linked list template <typename T> clinclass kedlist; /// node class template // template <typename T> class node {friend class clinkedlist <t>; private: T _ value; node <t> * _ pnext; public: node (void): _ pnext (null) {NULL;} explicit node (const T & Val): _ value (VAL), _ pnext (null) {NULL ;} T & getvalue (void) {return _ value;} Node <t> * getnext (void) {return _ pnext ;}}; // linked list class template // template <typename T> class clinkedlist {PRIVATE: node <t> * _ phead; public: clinkedlist (void );~ Clinkedlist (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> clinkedlist <t >:: clinkedlist (void) {_ phead = new node <t> () ;}template <typename T> clinkedlist <t> ::~ Clinkedlist (void) {clear (); Delete _ phead;} // clear all the nodes inserted in the linked list, except for the header node. // template <typename T> void clinkedlist <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 ;}} //// access the POs node data value at the specified position. If the POS is too large, an access out-of-bounds exception is thrown. // template <typename T> T & clinkedlist <t>: visit (const size_t POS) {node <t> * ptemp = NULL; ptemp = _ phead-> _ pnext; for (size_t I = 0; null! = Ptemp & I <Pos; ++ I) {ptemp = ptemp-> _ pnext;} If (null = ptemp) {Throw STD :: overflow_error ("linked list node access out of bounds! ");} Return ptemp-> _ value;} // calculate the total number of nodes and exclude the header node. // template <typename T> size_t clinkedlist <t>: length (void) const {size_t Len = 0; for (node <t> * ptemp = _ phead-> _ pnext; null! = Ptemp; ptemp = ptemp-> _ pnext) {++ Len;} return Len ;} /// Insert a new node after the head node of the linked list // template <typename T> node <t> * clinkedlist <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> * clinkedlist <t>: insert (const size_t POs, const T & Val) {node <t> * pnew = NULL, * pprev = NULL; pprev = _ phead; For (size_t I = 0; null! = Pprev-> _ pnext & I <Pos; ++ I) {pprev = pprev-> _ pnext;} 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> * clinkedlist <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> * clinkedlist <t>: Remove (const size_t POS) {node <t> * pprev = NULL, * pdel = NULL; pprev = _ phead; For (size_t I = 0; null! = Pprev-> _ pnext & I <Pos; ++ I) {pprev = pprev-> _ pnext;} If (null! = Pprev-> _ pnext) {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; clinkedlist <int> detail list; For (size_t I = 0; I <max_list_size; ++ I) {detail list. insert (I, 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 ;}