`

Linux下用C++实现支持任何类型的TList链表

阅读更多

 以下为tlist.h文件代码

 /* 
* 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); 
}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics