行业资讯 2025年06月7日
0 收藏 0 点赞 322 浏览 3761 个字
摘要 :

抽象数据类型(abstract date tpye, ADT) 1.抽象数据类型linearList C++支持两种类——抽象类和具体类。一个抽象类包含没有实现的代码的成员函数。这样的成员函数成为“虚……

抽象数据类型(abstract date tpye, ADT)

1.抽象数据类型linearList

C++支持两种类——抽象类和具体类。一个抽象类包含没有实现的代码的成员函数。这样的成员函数成为“虚函数(pure virtual function)”.

具体类是没有虚函数的类。只有虚函数才可以实例化(只能对具体类建立实例或对象)。但是可以建立抽象类的对象指针。

一个线性表的抽象类:

template<class T>

class linearList

{

    public:

        virtual ~linearList() {

        };

        //线性表为空,返回true 

        virtual bool empty() const  = 0;

        //返回线性表的元素个数 

        virtual int size() const = 0;

        //返回索引为theIndex的元素 

        virtual T& get(int theIndex) const = 0;

        //返回元素theElement第一次出现的索引 

        virtual int indexof(const T& theElement) const = 0;

        //删除索引为theIndex的元素 

        virtual void erase(int theIndex) = 0;

        //将元素theElement插入索引为theIndex的位置 

        virtual void insert(int theIndex, const T& theElement) = 0;

        //线性表插入输出流out 

        virtual void output(ostream& out) const = 0;

};

一个抽象类的派生类,只有实现了基类的所有纯虚函数才是具体类,否则依然是抽象类不能实例化。

抽象类linearList的派生类arrayList(具体类) :

template<class T>

class arrayList : public linearList<T>

{

    protected:

        T* element; //存储线性元素的一维数组 

        int arrayLength;    //一维数组的容量 

        int listSize;   //线性表的元素个数 

        //索引theIndex无效,则抛出异常 

        void checkIndex(int theIndex) const;

    public:

        //构造函数,复制函数和析构函数

        arrayList(int initialCapacity = 10);

        arrayList(const arrayList<T>&);

        ~arrayList()

        {

            delete [] element;

        }

        //DAT方法

        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 insetr(int theIndex, const T& theElement);

        void output(ostream& out) const;

        //其他方法

        int capacity() const

        {

            return arrayLength;

        }

};

类的具体实现:

template<class T>

arrayList<T>::arrayList(int initialCapacity)

{

    if(initialCapacity < 1)

    {

        ostringstream s;

        s << "初始容量 = " << initialCapacity << "必须大于零";   

        //throw illegalParameterValue(s.str());

    }

        arrayLength = initialCapacity;

        element = new T[arrayLength];

        listSize = 0;   

}

template<class T>

arrayList<T>::arrayList(const arrayList<T>& theList)

{

    arrayLength = theList.arrayLength;

    listSize = theList.listSize;

    element = new T[arrayLength];

    copy(theList.elemet, theList.element + listSize, element);

}

template<class T>

T& arrayList<T>::get(int theIndex) const

{

    checkIndex(theIndex);

    return element(theIndex); 

template<class T>

int arrayList<T>::indexof(const T& theElement) const

{

    int theIndex = (int) (find(element, element + listSize, theElement) – element);

    if(theIndex == listSize)

    {

        return -1;

    }

    else

    {

        return theIndex;

    }

}

template<class T>

void arrayList<T>::erase(int theIndex)

{

    //如果引索不存在,则抛出异常 

    checkIndex(theIndex);

    //引索存在,向前移动引索大于theIndex的元素

    copy(element + theIndex + 1, element + listSize, element + theIndex);

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

}

template<class T>

void arrayList<T>::insetr(int theIndex, const T& theElement) 

{

    if(theIndex < 0 || theIndex > listSize)

    {

        ostringstream s;

        s << "索引 = " << theIndex << "索引大小 = " << listSize;

        throw illeaglIndex(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) const

{

    copy(element, element + listSize, ostream_iterator<T>(cout, " "));

}

template<class T>

void arrayList<T>::checkIndex(int theIndex) const

{

    //确定索引theIndex在 0 到 listSize 之间

    if(theIndex < 0 || theIndex >= listSize)

    {

        ostringstream s;

        s << "索引 = " << theIndex << "索引大小 = " << listSize;

        throw illeaglIndex(s.str());    

    }

}

//改变一个一维数组长度

template<class T>

void changeLength1D(T*& a, int oldLength, int newLength)

{

    if(newLength < 0)

        cout<< "错误" <<endl; 

    T* temp = new T[newLength]; //新数组

    int number = min(oldLength, newLength);

    copy(a, a + number, temp);

    delete [] a;

    a = temp; 

}

库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

————————————————

原文链接:https://blog.csdn.net/peng_shakalaka/java/article/details/60598806


微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/4127.html

管理员

相关推荐
2025-06-07

在数字化阅读与实体书籍并存的时代,图书租赁管理系统正成为图书馆、书店及共享书吧提升运营效率的…

953
2025-06-07

在当今快节奏的校园生活中,外卖已成为学生和教职工日常生活中不可或缺的一部分。随着外卖需求的不…

997
2025-06-07

“金融市场瞬息万变,能否用Python实现同花顺自动化交易?” 这个问题,正是当下许多投资者和技术开发…

777
2025-06-07

你是否想过用中文编写一款高效、稳定的多用户聊天软件? 对于中小型团队或个人开发者而言,*易语言*…

1,018
2025-06-07

一、系统架构设计 分层架构: 前端层:用户端(H5/小程序/APP)+ 管理后台(Web) 服务层:抽奖核心…

674
2025-06-07

在数字化时代,二手交易市场正以前所未有的速度蓬勃发展。无论是闲置物品的流通,还是环保意识的提…

469
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号