VC中定义一个链表类
自已定义一个.h文件,然后,在里面加入类的定义
class CPartA : public CObject
{
public:
CPartA() {}
virtual ~CPartA() {}
int m_a;
int m_b;
CString m_c;
int m_d;
};
typedef CTypedPtrList<CObList, CPartA*> CPartAList;//重点在这儿
class CInfoA
{
public:
CInfoA() {}
virtual ~CInfoA();
BOOL AddData(int a, int b, CString c, int d); //用于加入数据的
CPartA m_objects;
};
在.cpp里
CInfoA的析构
CInfoA::~CInfoA()
{
POSITION pos = m_objects.GetHeadPosition();
while (pos != NULL)
delete m_objects.GetNext(pos);
}
//加入到链表
CInfoA::AddData(int a, int b, CString c, int d)
{
CPartA* pPart = new CPartA();
pPart->m_a = a;
pPart->m_b = b;
pPart->m_c = c;
pPart->m_d = d;
m_objects.AddTail(pPart);
return TRUE;
}
自已来引申,一定对你有用处的,当你在程序中要用到这个链表时
你只要在.h里定义
CInfoA m_InfoA
在.cpp里
m_Info.AddPart(1, 2, "ddd", 3);
就可以加入了
3.遍历这个链表
你在程序中的函数加入下面的语句即可
POSITION pos;
pos = m_InfoA.m_objects.GetHeadPosition();
while (pos != NULL)
{
CPartA* pPart = m_InfoA.m_objects.GetNext(pos);
if (pPart->m_a != 0)
........
}
- 上一篇:vc++启动和停止服务
- 下一篇:VC编程经验汇总