C++中CSimpleList的问题
C++中CSimpleList的实现与测试实例#include <windows.h>
#include <stddef.h>
#include<stdio.h>
class CSimpleList
{
public:
CSimpleList(int nNextOffset = 0);
void Construct(int nNextOffset);
// 提供给用户的接口函数(Operations),用于添加、删除和遍历节点
BOOL IsEmpty() const;
void AddHead(void* p);
void RemoveAll();
void* GetHead() const;
void* GetNext(void* p) const;
BOOL Remove(void* p);
// 为实现接口函数所需的成员(Implementation)
void* m_pHead; // 链表中第一个元素的地址
size_t m_nNextOffset; // 数据结构中pNext成员的偏移量
void** GetNextPtr(void* p) const;
};
// 类的内联函数
inline CSimpleList::CSimpleList(int nNextOffset)
{
m_pHead = NULL; m_nNextOffset = nNextOffset;
}
inline void CSimpleList::Construct(int nNextOffset)
{
m_nNextOffset = nNextOffset;
}
inline BOOL CSimpleList::IsEmpty() const
{
return m_pHead == NULL;
}
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 void** CSimpleList::GetNextPtr(void* p) const
{
return (void**)((BYTE*)p + m_nNextOffset);
}
void CSimpleList::AddHead(void* p)
{
*GetNextPtr(p) = m_pHead;
m_pHead = p;
}
BOOL CSimpleList::Remove(void* p)
{
if (p == NULL) // 检查参数
return FALSE;
BOOL bResult = FALSE; // 假设移除失败
if (p == m_pHead)
{
// 要移除头元素
m_pHead = *GetNextPtr(p);
bResult = TRUE;
}
else
{
// 试图在表中查找要移除的元素
void* pTest = m_pHead;
while (pTest != NULL && *GetNextPtr(pTest) != p)
pTest = *GetNextPtr(pTest);
// 如果找到,就将元素移除
if (pTest != NULL)
{
*GetNextPtr(pTest) = *GetNextPtr(p);
bResult = TRUE;
}
}
return bResult;
}
struct mythreaddata
{
mythreaddata* pnext;
int nsomedata;
};
void main()
{
mythreaddata* pdata;
CSimpleList list;
list.Construct(offsetof(mythreaddata, pnext)); // 告诉csimplelist类pnext成员的偏移量
// 向链表中添加成员
for (int i = 0; i < 10; i++)
{
pdata = new mythreaddata;
pdata->nsomedata = i;
list.AddHead(pdata);
}
// ………… // 使用链表中的数据
// 遍历整个链表,释放mythreaddata对象占用的空间
pdata = (mythreaddata*)list.GetHead();
while (pdata != NULL)
{
mythreaddata* pnextdata = pdata->pnext;
printf(" the value of nsomedata is: %d \n", pdata->nsomedata);
delete pdata;
pdata = pnextdata;
}
}
我在这个基础上加入c++的nullptr,和bool以及命名空间
#include<Windows.h>
namespace CWAF
{
namespace CWTLS
{
class CSimpleList
{
private:
void* m_pHead;
size_t m_nNextOffset;
void** GetNextPtr(void* p)const;
public:
explicit CSimpleList(int nNextOffset=0);
virtual ~CSimpleList()=default;
void Construct(int nNextOffset);
bool IsEmpty()const;
void AddHead(void* p);
void RemoveAll();
void* GetHead()const;
void* GetNext(void* p)const;
bool Remove(void* p);
};
}
}
#include "Cwaf.h"
namespace CWAF
{
namespace CWTLS
{
CSimpleList::CSimpleList(int nNextOffset )
{
m_pHead = nullptr;
m_nNextOffset = nNextOffset;
return;
}
void CSimpleList::Construct(int nNextOffset)
{
m_nNextOffset = nNextOffset;
return;
}
bool CSimpleList::IsEmpty()const
{
return m_pHead == nullptr;
}
void CSimpleList::RemoveAll()
{
m_pHead = nullptr;
}
void* CSimpleList::GetHead()const
{
return m_pHead;
}
void* CSimpleList::GetNext(void* preElement)const
{
return *GetNextPtr(preElement);
}
void** CSimpleList::GetNextPtr(void* p)const
{
return (void**)((BYTE*)p + m_nNextOffset);
}
void CSimpleList::AddHead(void* p)
{
*GetNextPtr(p) = m_pHead;
m_pHead = p;
}
bool CSimpleList::Remove(void* p)
{
if (p == nullptr)
return false;
bool bReslut = false;
if (p == m_pHead)
{
m_pHead = *GetNextPtr(p);
bReslut = true;
}
else
{
void* pTest = m_pHead;
while (pTest != nullptr && *GetNextPtr(pTest) != p)
pTest = *GetNextPtr(pTest);
if (pTest != nullptr)
{
*GetNextPtr(pTest) = *GetNextPtr(p);
bReslut = true;
}
}
return bReslut;
}
}
}
#include<stdio.h>
#include <stddef.h>
#include"Cwaf.h"
struct MyThreadData
{
MyThreadData* pNext;
int nSomeData;
};
int main()
{
MyThreadData* pData;
CWAF::CWTLS::CSimpleList list;
list.Construct(offsetof(MyThreadData, pNext));
for (int i = 0; i < 10; ++i)
{
pData = new MyThreadData;
pData->nSomeData = i;
list.AddHead(pData);
}
pData = (MyThreadData*)list.GetHead();
while (pData != nullptr);
{
MyThreadData* pNextDate = pData->pNext;
printf("The value of nSomeData is %d\n", pData->nSomeData);
delete pData;
pData = pNextDate;
}
return 0;
}
运行没结果