以下为tlist.h文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
/* * File: tlist.h * Author: root * * Created on 2008年12月1日, 下午9:46 */ #ifndef _TLIST_H #define _TLIST_H #include <list> class TList { public: TList(); TList(const TList& orig); virtual ~TList(); public: bool Add(void* data); bool Remove(void* data); bool IsEmpty(); bool Insert(int location,void* data); void* Front(); void* Last(); void* GetElem(int location); int Count(); void Free(); private: std::list<void*> list; }; #endif /* _TLIST_H */ 以下为实现tlist.cpp文件代码 /* * File: tlist.cpp * Author: root * * Created on 2008年12月1日, 下午9:46 */ #include "tlist.h" //------------------------------------------------------------ TList::TList() { } //------------------------------------------------------------ TList::TList(const TList& orig) { } //------------------------------------------------------------ //插入结点 bool TList::Add(void* data) { list.push_back(data); return true; } //------------------------------------------------------------ bool TList::Remove(void* data) { if(IsEmpty()) { return false; } else { list.remove(data); return true; } } //------------------------------------------------------------ bool TList::IsEmpty() { if(list.empty()) return true; else false; } //------------------------------------------------------------ bool TList::Insert(int location, void* data) { } //------------------------------------------------------------ void* TList::Front() { if(IsEmpty()) return NULL; else return list.front(); } //------------------------------------------------------------ void* TList::Last() { if(IsEmpty()) return NULL; else return list.back(); } //------------------------------------------------------------ void* TList::GetElem(int location) { } //------------------------------------------------------------ int TList::Count() { int count = 0; if(IsEmpty()) count = 0; else count = list.size(); return count; } //------------------------------------------------------------ void TList::Free() { list.clear(); } //------------------------------------------------------------ TList::~TList() { Free(); } //------------------------------------------------------------ 以下为newmain.cpp测试代码 /* * File: newmain.cpp * Author: root * * Created on 2008年12月3日, 下午9:09 */ #include <stdlib.h> #include "tlist.h" #include <iostream> using namespace std; /* * */ typedef struct { int a; char item[1024]; }Data; int main(int argc, char** argv) { TList* list = new TList(); Data* data = new Data(); data->a = 10; strcpy(data->item,"abc"); bool status = list->Add((void*)data); if(status) cout << "Add data successful" << endl; else cout << "Add data failure" << endl; Data*d = new Data(); d->a = 100; strcpy(d->item,"ddddddddd"); status = list->Add((void*)d); if(status) cout << "Add data successful" << endl; else cout << "Add data failure" << endl; Data* data1; data1 = (Data*) list->Front(); cout << "a = " << data1->a << endl; cout << "item = " << data->item << endl; Data* d1; d1 = (Data*) list->Last(); cout << "a = " << d1->a << endl; cout << "item = " << d1->item << endl; list->Remove(data); cout << "count = " << list->Count() << endl; Data* dd; dd = (Data*)list->Front(); cout << "a = " << dd->a << endl; cout << "item = " << dd->item << endl; return (EXIT_SUCCESS); } |
发表评论
要发表评论,您必须先登录。