Implementation and test instance of Csimplelist in C + + _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.