- #include "stdafx.h"
- #include <stddef.h>
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- //MFC中链表的实现
- class CMySimpleList
- {
- public:
- CMySimpleList(int nNextOffset = 0);
- void Construct(int nNextOffset);
- BOOL IsEmpty(void) const;
- void AddHead(void* p);
- void RemoveAll(void);
- void* GetHead(void) const;
- void* GetNext(void* p) const;
- BOOL Remove(void* p);
- public:
- void* m_pHead;
- size_t m_nNextOffset;
- void** GetNextPtr(void* p) const;
- };
- CMySimpleList::CMySimpleList(int nNextOffset) : m_nNextOffset(nNextOffset), m_pHead(NULL)
- {
- }
- void CMySimpleList::Construct(int nNextOffset)
- {
- m_nNextOffset = nNextOffset;
- }
- BOOL CMySimpleList::IsEmpty() const
- {
- return (NULL == m_pHead);
- }
- void CMySimpleList::AddHead(void* p)
- {
- *GetNextPtr(p) = m_pHead;
- m_pHead = p;
- }
- void* CMySimpleList::GetHead(void) const
- {
- return m_pHead;
- }
- void* CMySimpleList::GetNext(void* p) const
- {
- return *GetNextPtr(p);
- }
- BOOL CMySimpleList::Remove(void* p)
- {
- BOOL bRet = FALSE;
- if (NULL == p)
- {
- goto Exit0;
- }
- if (p == m_pHead)
- {
- m_pHead = *GetNextPtr(p);
- bRet = TRUE;
- }
- else
- {
- void* pTmp = m_pHead;
- while (pTmp != NULL && *GetNextPtr(pTmp) != p)
- {
- pTmp = *GetNextPtr(pTmp);
- }
- if (pTmp != NULL)
- {
- *GetNextPtr(pTmp) = *GetNextPtr(p);
- bRet = TRUE;
- }
- }
- Exit0:
- return bRet;
- }
- void CMySimpleList::RemoveAll(void)
- {
- m_pHead = NULL;
- }
- void** CMySimpleList::GetNextPtr(void* p) const
- {
- return (void**)((BYTE*)p + m_nNextOffset);
- }
- //对上面的代码再进行一次封装
- template<class TYPE>
- class CMyTypedSimpleList : public CMySimpleList
- {
- public:
- CMyTypedSimpleList(int nNextOffset = 0) : CMySimpleList(nNextOffset)
- {
- }
- TYPE GetHead(void)
- {
- return (TYPE)CMySimpleList::GetHead();
- }
- TYPE GetNext(TYPE p)
- {
- return (TYPE)CMySimpleList::GetNext((void*)p);
- }
- BOOL Remove(TYPE p)
- {
- return CMySimpleList::Remove((void*)p);
- }
- operator TYPE()
- {
- return (TYPE)CMySimpleList::GetHead();
- }
- };
- struct MyData
- {
- MyData* pNext;
- int nSomeData;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- /*
- CMySimpleList myList;
- MyData* pMyData = NULL;
- myList.Construct(offsetof(MyData, pNext));
- for (int i = 0; i < 10; i++)
- {
- pMyData = new MyData;
- pMyData->nSomeData = i;
- myList.AddHead(pMyData);
- }
- pMyData = (MyData*)myList.GetHead();
- while(pMyData != NULL)
- {
- MyData* pTmp = pMyData->pNext;
- cout << pMyData->nSomeData << endl;
- delete pMyData;
- pMyData = pTmp;
- }
- */
- CMyTypedSimpleList<MyData*> myList;
- MyData* pMyData = NULL;
- myList.Construct(offsetof(MyData, pNext));
- for (int i = 0; i < 10; i++)
- {
- pMyData = new MyData;
- pMyData->nSomeData = i;
- myList.AddHead(pMyData);
- }
- //pMyData = myList.GetHead();
- pMyData = myList;
- while(pMyData != NULL)
- {
- MyData* pTmp = pMyData->pNext;
- cout << pMyData->nSomeData << endl;
- delete pMyData;
- pMyData = pTmp;
- }
- return 0;
- }
MFC中一个链表的实现
最新推荐文章于 2022-01-04 11:24:13 发布