顺序表

本文详细介绍了如何使用模板类实现动态数组,并提供了插入、删除、获取位置、设置值等关键操作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<bits/stdc++.h>
using namespace std;
int n;
template < class T>

class arrList
{
    private:
        T *aList;
        int maxSize;
        int curLen;
        int position;

    public:
        arrList(const  int s)
        {
            maxSize=s;
            aList=new T[maxSize];
            curLen=position=0;
        }
        ~arrList()
        {
            delete[] aList;
        }
        void Clear()
        {
            delete []aList;
            curLen=position=0;
            aList=new T[maxSize];
        }
        int length();
        bool Append(const T value); //表尾添加元素
        bool Insert(const int p,const T value);
        bool Delete(const int p);
        bool getValue(const int p, T &value);
        bool setValue(const int p,const T value);
        bool getPos(int &p,const T value);

};

template <class T>
bool arrList<T>::getPos(int &p,const T value)
{
    int i;
    for(i=0;i<n;i++)
        if(value == aList[i])
    {
        p=i;
        return 1;
    }
    return 0;
}

template <class T>
bool arrList<T>::Insert(const int p,const T value)
{
    int i;
    if(curLen>=maxSize)
    {
        cout<<"The list is overflow!"<<endl;
        return 0;
    }
    if(p<0||p>curLen)
    {
        cout<<"Insertion point is illegal"<<endl;
        return 0;
    }
    for(i=curLen;i>p;i--)
        aList[i]=aList[i-1];
    aList[p]=value;
    curLen++;
    return 1;
}

template <class T>
bool arrList<T>::Delete(const int p)
{
    int i;
    if(curLen<=0)
    {
        cout<<"No element to delete\n"<<endl;
        return 0;
    }
     if(p<0||p>curLen-1)
    {
        cout<<"deletion is illegal\n"<<endl;
        return 0;
    }
    for(i=p;i<curLen-1;i++)
        aList[i]=aList[i+1];
    curLen--;
    return 1;
}


int main()
{
    return 0;
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值