读书人

请教这样实现向量可以不?有有关问题吗

发布时间: 2012-02-21 16:26:23 作者: rapoo

请问这样实现向量可以不??有问题吗??

C/C++ code
#include <iostream>using namespace std;const int LIST_INIT_SIZE=100;const int LISTINCREMENT=10;template <class Object>struct Node{    Object data;    bool operator == (Node<Object> vs)    {        if(this.data==vs.data)            return true;        else            return false;    }};template <class Object>class List{public:    List();    List(List<Object> &l);    ~List();    bool InitList();    bool DestroyList();    bool ClearList();    bool ListEmpty();    int  ListLength();    bool GetElem(int i,Node<Object> *e);//    bool LocateElem(Node<Object> e,    bool PriorElem(Node<Object> cur_e,Node<Object> *pre_e);    bool NextElem(Node<Object> cur_e,Node<Object> *next_e);    bool ListInsert(int i,Node<Object> e);    bool ListDelete(int i,Node<Object> *e);//    bool ListTraverse(vist());private:    Node<Object> *pData;    int iCurLength;    int iMaxLength;};template <class Object>List<Object>::List(){    InitList();}template <class Object>List<Object>::List(List<Object> &l){    iCurLength=l.iCurLength;    iMaxLength=l.iMaxLength;    if(pData)        delete pData;    pData=new Node<Object>[iMaxLength];    pData=l.pData;}template <class Object>List<Object>::~List(){    ClearList();    DestroyList();}template <class Object>bool List<Object>::InitList(){    pData=new Node<Object>[LIST_INIT_SIZE];    if(!pData)        return false;    iCurLength=0;    iMaxLength=LIST_INIT_SIZE;    return true;}template <class Object>bool List<Object>::ClearList(){    if(pData)        delete pData;    pData=new Node<Object>[LIST_INIT_SIZE];    if(!pData)        return false;    iCurLength=0;    iMaxLength=LIST_INIT_SIZE;    return true;}template <class Object>bool List<Object>::DestroyList(){    if(pData)        delete pData;    iCurLength=0;    iMaxLength=0;    return true;}template <class Object>bool List<Object>::GetElem(int i,Node<Object> *e){    if(i<0 || i>iCurLength)    {        e=NULL;        return false;    }    *e=pData[i];    return true;}template <class Object>bool List<Object>::PriorElem(Node<Object> cur_e,Node<Object> *pre_e){    for(int i=0;i<iCurLength;i++)    {        if(pData[i]==cur_e && i!=0)        {            *pre_e=pData[i-1];            return true;        }    }    pre_e=NULL;    return false;}template <class Object>bool List<Object>::NextElem(Node<Object> cur_e,Node<Object> *next_e){    for(int i=0;i<iCurLength;i++)    {        if(pData[i]==cur_e && i!=iCurLength-1)        {            *next_e=pData[i+1];            return true;        }    }    next_e=NULL;    return false;}template <class Object>bool List<Object>::ListInsert(int i,Node<Object> e){    if(iCurLength==iMaxLength)    {        pData=new Node<Object>[iMaxLength+LISTINCREMENT];        if(!pData)        {            return false;        }        iMaxLength+=LISTINCREMENT;    }    iCurLength++;    for(int index=iCurLength-1;index>i;i--)    {        pData[index+1]=pData[index];    }    pData[i]=e;    return true;}template <class Object>bool List<Object>::ListDelete(int i,Node<Object> *e){    if(iCurLength==0)        return false;    *e=pData[i];    for(int index=i;index<iCurLength;i++)    {        pData[i]=pData[i+1];    }    return true;}template <class Object>bool List<Object>::ListEmpty(){    if(iCurLength==0)        return true;    return false;}template <class Object>int List<Object>::ListLength(){    return iCurLength;}


------解决方案--------------------


不错,数据结构就应该用C++的模板来做,用C的自定义类型感觉还是太麻烦了点!!

读书人网 >软件架构设计

热点推荐