There are a lot of code for inserting and deleting nodes, but this increases the readability of the code without increasing the time complexity and does not affect program performance.
# Include <iostream> using namespace STD; Template <typename T> class clist; Template <class T> class node {friend clist <t>; private: T m_data; node * m_pnext;}; Template <class T> class clist {public: clist ();~ Clist (); bool isempty (); void append (const T & data); void Delete (const Int & Pos); void print (); int getlength (); t find (const Int & Pos); void insert (const Int & Pos, const T & data); Private: node <t> * m_phead; node <t> * m_pend; int m_len; void create (); void destroy () ;}; // allocate a space template for the header node <class T> void clist <t >:: create () {m_phead = new node <t>; m_pend = new node <t>; m_phead-> m_pnext = NULL; m_pend-> m_pnext = m_phead-> M _ Pnext; m_len = 0;} template <class T> clist <t >:: clist () {create ();} // Delete All node templates <class T> void clist <t>: Destroy () {node <t> * pF = m_phead-> m_pnext; node <t> * PT; while (PF) {Pt = PF; pF = PF-> m_pnext; Delete PT ;}}template <class T> clist <t> ::~ Clist () {destroy () ;}// determines whether the template is empty. <class T> bool clist <t >:: isempty () {If (! M_phead-> m_pnext) {return true;} else {return false ;}// add an element template from the end of the table <class T> void clist <t> :: append (const T & Data) {node <t> * PT = new node <t>; Pt-> m_data = data; Pt-> m_pnext = NULL; If (! M_phead-> m_pnext) {m_phead-> m_pnext = pt;} else {(m_pend-> m_pnext)-> m_pnext = pt;} m_pend-> m_pnext = pt; + + m_len;} // delete an element template <class T> void clist <t>: delete (const Int & Pos) {If (Pos <0 | POS <m_len) {cout <"the location is invalid" <Endl; return;} node <t> * PPRE = NULL; // store the previous node <t> * pbehind = NULL; // store the last node <t> * PT = m_phead-> m_pnext; // destination node int IX =-1; while (PT) {++ IX; If (IX = pos-1-1) {PPRE = pt ;} else if (IX = Pos-1) {pbehind = Pt-> m_pnext; break;} Pt = Pt-> m_pnext;} If (! PPRE) // if the pointer is null, POS indicates the first element {Delete pt; m_phead-> m_pnext = pbehind; -- m_len; return;} If (! Pbehind) // if the pointer is null, the POs indicates the last element {m_pend = PPRE; Delete pt;} PPRE-> m_pnext = pbehind; -- m_len ;} // output all data templates <class T> void clist <t>: Print () {node <t> * PT = m_phead-> m_pnext; while (PT) {cout <Pt-> m_data <","; Pt = Pt-> m_pnext;} cout <Endl;} template <class T> int clist <t> :: getlength () {return m_len;} // search for the data template <class T> T clist <t>: Find (const Int & Pos) {If (Pos <= 0) {cout <"invalid input" <Endl; return NULL;} If (Pos> M_L En) {cout <"exceeds the table length" <Endl; return NULL;} int I = 0; node <t> * PT = m_phead-> m_pnext; while (PT) {++ I; if (I = POS) {return Pt-> m_data;} Pt = Pt-> m_pnext;} return NULL ;} template <class T> void clist <t>: insert (const Int & Pos, const T & Data) {If (Pos <= 0 | POS> m_len) {cout <"invalid input" <Endl; return;} int I = 0; node <t> * PT = m_phead-> m_pnext; node <t> * PPRE = NULL; node <t> * pbehind = NULL; while (PT) {++ I; if (I = pos-1) {PPRE = Pt;} if (I = POS) {pbehind = Pt-> m_pnext; break;} Pt = Pt-> m_pnext ;} node <t> * pnew = new node <t>; pnew-> m_data = data; If (! PPRE) // if the pointer is null, POS indicates the first element {pnew-> m_pnext = m_phead-> m_pnext; m_phead-> m_pnext = pnew; ++ m_len; return;} If (! Pbehind) // if the pointer is null, POS indicates the last element {m_pend-> m_pnext = pnew;} PPRE-> m_pnext = pnew; pnew-> m_pnext = pt; + + m_len ;}