//-Dynalist.h#ifndef Dynalist_h # define Dynalist_H
Template Class Dynalist (); ~ Dynalist (); public: // Add a node int Add (TBJSRC); T & Operator [】; int size (); private: struct Tagnode {// Object T t t t at; // is the first time to initialize Bool bfirst; // Node index value int NINDEX; // Next node pointer tagnode * pnext; // front a node pointer tagnode * Pbefore }; Private: // node temporary pointer tagnode * m_ptem; // Link head pointer tagnode * m_phead; // New node pointer tagnode * m_pnew; int m_nsize;};}; "Dynalist.cpp # incrude" Dynalist. H "
#include #include #include #include
Using namespace std;
Template Dynalist :: Dynalist () {// Initialization Data M_PHEAD = NULL; M_PNEW = NULL; M_PHEAD = New Tagnode; m_phead-> nindex = -1; m_phead-> bfirst = true; m_phead-> Pbefore = null; m_phead-> pnext = null; m_nsize = 0;}
// Add Node Template int Dynalist :: add (t objsrc) {// Set a traversal pointer m_ptem = m_phead; // Move the traversal to the last node while (m_ptem-> pnext! = NULL) {m_ptem = m_ptem-> pnext;} // new node processing m_pnew = new tagnode; m_pnew-> pbefore = m_ptem; m_pnew-> pnext = null; m_pnew-> bfirst = true; m_pnew-> nindex = m_ptem -> NINDEX 1; // Content Copy Memcpy (& M_PNEW-> T, & Objsrc, SizeOf (OBJSRC)); m_ptem-> pnext = m_pnew; // Get the first node m_pHead = m_ptem; m_nsize; return m_pnew-> nindex }
Template Int Dynalist :: Size () {Return M_NSize;}
Template T & Dynalist :: Operator [】 (INT NINDEX) {IF (Nindex <0) {throw Exception ("IT IS Not INDEX!");}
IF (Nindex> M_NSize - 1) {throw exception ("IT is out of the range!");}
While (m_phead-> pnext! = null) {if (m_phead-> nindex == nindex) {return m_phead-> t;} m_phead = m_phead-> pnext;}
Return m_phead-> t;}
Template Dynalist :: ~ Dynalist () {while (m_phead-> pnext! = null) {m_phead = m_phead-> pnext;} // A release of the node to while (m_phead-> pbefore! = Null) {tagnode * ptem = m_phead; m_phead = m_phead-> pbefore; delete ptem;}}