顺序表应用实例

#include "stdafx.h" 


template <class T>
class arrList
{                                       // 顺序表,向量
private:                               // 线性表的取值类型和取值空间
    T *aList ;                        // 私有变量,存储顺序表的实例
    int maxSize;                      // 私有变量,顺序表实例的最大长度
    int curLen;                        // 私有变量,顺序表实例的当前长度
    int position;                       // 私有变量,当前处理位置
public:                                // 顺序表的运算集
    arrList(const int size) 
    {                                  // 创建一个新的顺序表,参数为表实例的最大长度
        maxSize = size; aList = new T[maxSize]; curLen = position = 0;
    }
    ~arrList() 
    {                                   // 析构函数,用于消除该表实例
        delete [] aList;
        cout<<"析构函数"<<endl;
    }
    void clear() 
    {                                        // 将顺序表存储的内容清除,成为空表
        delete [] aList; curLen = position = 0;
        aList = new T[maxSize];
    }
    int length();                                      // 返回此顺序表的当前实际长度
    bool append(const T value);                        // 在表尾添加一个元素value,表的长度增1
    bool insert(const int p, const T value);           // 在位置p上插入一个元素value,表的长度增1
    bool delete1(const int p);                          // 删除位置p上的元素,表的长度减 1
    bool setValue(const int p, const T value);         // 用value修改位置p的元素值
    bool getValue(const int p, T& value);               // 把位置p的元素值返回到变量value中 
    bool getPos(int & p, const T value);               // 查找值为value的元素,并返回第1次出现的位置
    void output();
};


template <class T>
int arrList<T>::length()
{
    return curLen;
}


template <class T>
bool arrList<T>::append(const T value)
{
    aList[curLen++]=value;
    return true;
}


template <class T>
bool arrList<T>::insert(const int p, const T value)       // 在位置p上插入一个元素value,表的长度增1
{
    if(curLen>=maxSize)
        cout<<"The list is overflow!"<<endl;
    if(p<0||p>=maxSize)
        cout<<"The position is illegal!"<<endl;
    for(int i=curLen-1;i>=p;i--)
        aList[i+1]=aList[i];
    curLen++;
    aList[p]=value;
    return true;
}


template <class T>
bool arrList<T>::delete1(const int p)                        // 删除位置p上的元素,表的长度减 1
{
    if(curLen>=maxSize)
        cout<<"The list is overflow!"<<endl;
    if(p<0||p>=maxSize)
        cout<<"The position is illegal!"<<endl;
    for(int i=p;i<curLen-1;i++)
        aList[i]=aList[i+1];
    curLen--;
    return true;
}


template <class T> bool arrList<T>::setValue(const int p, const T value)         // 用value修改位置p的元素值
{
    if(curLen>=maxSize)
        cout<<"The list is overflow!"<<endl;
    if(p<0||p>maxSize)
        cout<<"The position is illegal!"<<endl;
    aList[p]=value;
    return true;
}


template <class T>
bool arrList<T>::getValue(const int p, T& value)               // 把位置p的元素值返回到变量value中 
{
    if(curLen>=maxSize)
        cout<<"The list is overflow!"<<endl;
    if(p<0||p>maxSize)
        cout<<"The position is illegal!"<<endl;
    value=aList[p];
    return true;
}


template <class T>
bool arrList<T>::getPos(int & p,const T value)                 // 查找值为value的元素,并返回第1次出现的位置
{
    if(curLen>=maxSize)
        cout<<"The list is overflow!"<<endl;
    for(int i=0;i<curLen;i++)
    {
        if(aList[i]==value)
        {
            p=i;
            break;
        }
    }
    return true;
}


template <class T>
void arrList<T>::output()                                 //输出顺序表函数
{
    for(int i=0;i<length();i++)
    {
        T value;
        getValue(i,value);
        cout<<value<<" ";
    }
    cout<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值