线性表--数组描述(封装类)

抽象类linearList


template<class T>
class linearList
{
public:
    //全部都是纯虚函数
    virtual ~linearList(){};
    virtual bool empty() const=0;
                //返回true,当且仅当线性表为空
    virtual int size() const=0;
                //返回线性表的元素个数
    virtual T& get(int theIndex) const=0;
                //返回索引为theIndex的元素
    virtual int indexOf(const T& theElement) const=0;
                //返回元素theElement第一次出现时的索引
    virtual void erase(int theIndex) =0;
                //删除索引为theIndex的元素
    virtual void insert(int theIndex,const T& theElement) =0;
                //把theElement插入线性表中索引为theIndex的位置上
    virtual void output(ostream& out) const=0;
                //把线性表插入输出流out
};




//类arrayList(由抽象类linearList派生而来)


template<class T>
class arrayList:public linearList<T>
{
public:
    //构造函数、复制构造函数和析构函数
    arrayList(int initialSize=100);
    arrayList(const arrayList<T>& );
    ~arrayList(){delete []element;}


    //ADT方法
    bool empty() const
    {
        return listSize==0;
    }
    int size() const
    {
        return listSize;
    }
    T& get(int theIndex) const;
    int indexOf(const T& theElement) const;
    void erase(int theIndex);
    void insert(int theIndex,const T& theElement);
    void output(ostream& out) const;


    //其它方法
    int capacity() const
    {
        return arrayLength;
    }


protected:
    void checkIndex(int theIndex) const;
            //若索引theIndex无效,则抛出异常
    T* element;//存储线性表元素的一维数组
    int arrayLength;//一维数组的容量
    int listSize;//线性表的元素个数
};



剩余函数实现:
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{//构造函数
    if(initialCapacity < 1)
    {
        ostringstream s;
        s<<"Initial Capacity = "<<initialCapacity<<"Must be > 0";
        throw illegalParameterValue(s.str());
    }
    arrayLength = initialCapacity;
    element = new T[initialCapacity];
    listSize = 0;
}



template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{//复制构造函数
    arrayLength = theList.arrayLength;
    listSize = theList.listSize;
    element = new T[listSize];
    copy(theList.element,theList.element+listSize,element);
}



template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{//确定索引theIndex在0和listSize - 1之间
    if(theIndex < 0 || theIndex >= listSize)
    {
        ostringstream s;
        s << "index = " << theIndex << " size = " << listSize;
        throw illegalIndex(s.str());
    }
}



template<class T>
T& arrayList<T>::get(int theIndex) const
{//返回索引为theIndex的元素
    checkIndex(theIndex);
    return element[theIndex];
}



template<class T>
int arrayList<T>::indexOf(const T& theElement) const
{//返回元素theElement第一次出现时的索引
    //查找
    int res=(int)(find(element, element + listSize, theElement) - element);
    if(res==listSize)
        return -1;
    else
        return res;
}




template<class T>
void arrayList<T>::erase(int theIndex)
{//删除其索引为theIndex的元素
    //若不存在,则抛出异常
    checkIndex(theIndex);


    //移动
    copy(element + theIndex + 1,element + listSize,element + theIndex);


    element[--listSize].~T();   //调用析构函数
}




//该函数改变一个一维数组的长度,不属于某个类
void changeLength1D(T*& a,int oldLength,int newLength)
{
    if(newLength < 0)
        throw illegalParameterValue("new length must be >= 0");


    T* temp = new T[newLength];
    int number = min(oldLength,newLength);
    copy(a,a+number,temp);
    delete []a;
    a=temp;
}





template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement)
{//在索引theIndex处插入
    //无效索引
    if(theIndex < 0 || theIndex > listSize)
    {
        ostringstream s;
        s << "index = " << theIndex << " size = " << listSize;
        throw illegalIndex(s.str());
    }
    //有效索引,确定数组是否已满
    if(listSize==arrayLength)
    {//已满,数组长度倍增
        changeLength1D(element, arrayLength, 2*arrayLength);
        arrayLength*=2;
    }


    //把元素向右移动一个位置
    copy_backward(element + theIndex,element + listSize,element + theIndex + 1);
    element[theIndex]=theElement;
    listSize++;
}


template<class T>
void arrayList<T>::output(ostream& out)
{//把线性表插入输出流
    copy(element,element + listSize,ostream_iterator<T>(cout," "));
}
//重载<<
template<class T>
ostream& operator<<(ostream& out,const arrayList<T>& x)
{
    x.output(out);
    return out;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值