C++ LIST實例

#include using namespace std; //(0)定義相關類型 typedef struct { int id; char name[20] ; int age; }T_student; // student列表類型定義 typedef list< T_student*, allocator > LSTSTUDENT; typedef list< T_student*, allocator >::iterator ITSTU; LSTSTUDENT stuList; void testAddLIST() { T_student * pStu=NULL; for(int i=0;i<3;i++) { pStu=new T_student(); memset(pStu,0,sizeof(T_student)); pStu->id=i+1; strcpy(pStu->name,"name"); pStu->age=20+i; stuList.push_back(pStu); } } void printAll() { printf("All data is:/n"); ITSTU itStu; for (itStu = stuList.begin(); itStu != stuList.end(); itStu++) { T_student * pTemp=*itStu; printf("/tid=%d/tname=%s/tage=%d/n",pTemp->id,pTemp->name,pTemp->age); } } void deleteAll() { ITSTU itStu; for (itStu = stuList.begin(); itStu != stuList.end(); itStu++) { T_student * pTemp=*itStu; delete pTemp; pTemp=NULL; } stuList.clear(); } void deleteOne(int id) { ITSTU itStu; for (itStu = stuList.begin(); itStu != stuList.end(); itStu++) { T_student * pTemp=*itStu; if(pTemp->id==id) { stuList.remove(pTemp); delete pTemp; pTemp=NULL; break; } } } T_student * getOne(int id) { ITSTU itStu; for (itStu = stuList.begin(); itStu != stuList.end(); itStu++) { T_student * pTemp=*itStu; if(pTemp->id==id) { return pTemp; } } return NULL; } void main() { testAddLIST(); printAll(); deleteOne(1); printAll(); deleteAll(); printAll(); }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章