The example in this article describes the implementation of the C + + Simple List class. Share to everyone for your reference. The specific methods are as follows:
_afxtls. The CPP file is as follows:
#include "StdAfx.h #include <stddef.h> #include <stdio.h> #include" _afxtls_.
H "struct mythreaddata{mythreaddata* pnext;
int nshortdata;
};
void Csimplelist::addhead (void *p) {*getnextptr (P) =m_phead;
M_phead = p;
BOOL Csimplelist::remove (void* p) {bool bRet = FALSE;
if (p = = NULL) {bRet = FALSE;
} if (p = = m_phead) {m_phead = GetNext (M_phead);
BRet = TRUE;
else {void* ptest;
Ptest = M_phead;
while (Ptest && (GetNext (ptest)!= p)) {ptest = GetNext (ptest);
} if (Ptest!= NULL) {*getnextptr (ptest) = GetNext (p);
BRet = TRUE;
} return BRet;
} void Main () {mythreaddata* pData;
Csimplelist list; List.
Construct (Offsetof (Mythreaddata, Pnext));
for (int i=0;i<10;i++) {pData = new Mythreaddata;
Pdata->nshortdata = i; List.
AddHead (PData);
}//Traverse the list to release the space occupied by the Mythreaddata object PData = (mythreaddata*) list.
GetHead ();
while (PData!= NULL) {mythreaddata* Pnextdata = pdata->pnext;
printf ("The value is%d\n", pdata->nshortdata);
Delete PData;
PData = Pnextdata; }
}
_afxtls_. The H file is as follows:
#include "StdAfx.h #ifndef __afxtls_h__ #define __AFXTLS_H__ #include <Windows.h> class Csimplelist {Publi
c:csimplelist (int nnextoffset=0);
void construct (int nnextoffset);
Interface BOOL IsEmpty () const;
void AddHead (void *p);
void RemoveAll ();
void* gethead () const;
void* GetNext (void* preelement) const;
BOOL Remove (void* p);
The member void *m_phead required to implement the interface;
size_t M_nextoffset;
void** getnextptr (void* preelement) const;
}; The inline function of the class inline csimplelist::csimplelist (int nnextoffset) {m_phead = NULL; m_nextoffset = Nnextoffset; } inline void csimplelist::construct (int nnextoffset) {m_nextoffset = Nnextoffset;
} inline BOOL Csimplelist::isempty () const {return m_phead==null;
}//inline void AddHead (void *p)//{////} inline void Csimplelist::removeall () {m_phead = NULL;
} inline void* Csimplelist::gethead () const {return m_phead;
} inline void* Csimplelist::getnext (void* preelement) const {Return *getnextptr (preelement); }//inline BOOL Csimplelist::remove (void* p)//{////} inline void** csimplelist::getnextptr (void* preelement) cons
T {return (void**) ((byte*) preelement+m_nextoffset);
} #endif
I hope this article will help you with your C + + programming.