list列表的使用。有点问题,帮着给看看

需要面对面帮助?请寻求当地校园社团支持!
回复
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

list列表的使用。有点问题,帮着给看看

#1

帖子 weihua2008 » 2009-04-27 18:01

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<list>
using namespace std;

struct CTest
{
int n;
char *p;
};
typedef list<CTest *> CTestList;

CTest * AllocCTest();
bool RemoveTest(CTest *pCTest);
int main()
{
CTestList TestList;

CTest *pCtest1=AllocCTest();
CTest *pCtest2=AllocCTest();
CTest *pCtest3=AllocCTest();

pCtest1->n=1;
pCtest1->p=new char [14];
memset(pCtest1->p,0,sizeof(pCtest1->p));
strncpy(pCtest1->p,"one",strlen("one"));

pCtest2->n=2;
pCtest2->p=new char [14];
memset(pCtest2->p,0,sizeof(pCtest2->p));
strncpy(pCtest2->p,"two",strlen("two"));

pCtest3->n=3;
pCtest3->p=new char [14];
memset(pCtest3->p,0,sizeof(pCtest3->p));
strncpy(pCtest3->p,"three",strlen("three"));

TestList.push_front(pCtest1);
TestList.push_front(pCtest2);
TestList.push_front(pCtest3);

list<CTest *>::const_iterator iter;
iter=TestList.begin();//
while(iter!=TestList.end())
{
DeleteTest(*iter);
iter=TestList.begin();

}

//TestList.clear

bool bRet=TestList.empty();
if(true==bRet)
{
cout<<"现在队列为空"<<endl;
}
else
{
cout<<"现在队列不为空"<<endl;
}

TestList.clear();

return 0;
}
//分配结构体对象
CTest * AllocCTest()
{
CTest *pCTest=new CTest();
pCTest->n=0;
pCTest->p=NULL;

return pCTest;
}
//删除结构体对象
bool DeleteTest(CTest *pCTest)
{
if(NULL==pCTest)
return false;
pCTest->n=0;
if(pCTest->p!=NULL)
{
delete [] pCTest->p;
pCTest->p=NULL;
}

delete pCTest;
return true;
}
回复