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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值