wwtsanmao 发表于 2009-12-21 16:41:31

list1.h

#ifndef TLIST_H
#define TLIST_H
#define SLOP_OVER_INOF PRINT("越界!返回最后一个成员!");
#include "base.h"
template <class T>
class tList
{
private:
T    *units;      //动态分配的数组
int   numofMax;   //当前分配的元素的最大数目
intnumofUnit;//当前存放的元素的实际数目
intfreep;      //Next()函数使用的标记
public:

tList()
{
Reset();
}
tList(tList& _obj)
{
Reset();
int count = _obj.Count();
for(int i = 0;i < count;i++)
{
   Assign(_obj.Get(i));
}
}
~tList()
{
Free();
Reset();
}
protected:
/*########################*/
//void Reset()
//重置成员变量
/*########################*/
void Reset()
{
units = NULL;
numofMax = 0;
numofUnit = 0;
freep = 0;
return;
}
/*########################*/
//void Free()
//释放内存
/*########################*/
void Free()
{
if(units != NULL)
{
   DELETE_P(units);
}
}
private:
/*########################*/
//void Extend(int numofInc)
//扩展内存空间
/*########################*/
void Extend(int numofInc)
{
if (numofInc == NULL)
{
   numofInc = sizeof(T);
}
      if (units == NULL)
      {
            //第一次
            NEW(units,T);
            numofMax = numofInc;
      }
      else
      {
            numofInc += numofMax;
   T *tmp;
   NEW(tmp,T);
            CopyElements(tmp, units, (sizeof(T) * numofUnit));
            Free();   
            units = tmp;
   tmp = NULL;
            numofMax = numofInc;
      }
return;
}
/*########################*/
//void Insert(const T& _value,int _pos)
//向链表中插入成员
/*########################*/
void Insert(const T& _value,int _pos)
{
if(_pos < 0)
{
   PRINT("parameter \"_pos\" error!");
   return;
}
Extend(1);
if(_pos >= numofUnit)
{
   _pos=numofUnit;
}
else
{
   CopyElements(&units, &units, (sizeof(T) * (numofUnit - _pos)));
}
units = _value;
numofUnit++;
return;
}
public:
/*########################*/
//T Get(int _pos)
//取得链表中指定的成员
/*########################*/
T Get(int _pos) const
{
if (_pos >= numofUnit)
{
   SLOP_OVER_INOF;
   _pos = numofUnit - 1;
}
return units;
}
/*########################*/
//void Assign(const T& _value)
//向链表中追加成员
/*########################*/
void Assign(const T& _value)
{
Insert(_value,Count());
return;
}

我是别人的马甲 发表于 2009-12-24 23:35:45

你要干嘛?
页: [1]
查看完整版本: list1.h