线性表(数组实现)

#include <bits/stdc++.h>

template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
    if (newLength < 0) {
        throw "error";
    }
    T *temp = new T[newLength];
    int number = std::min(oldLength, newLength);
    std::copy(a, a + number, temp);
    delete[]a;
    a = temp;
}

template<class T>
class linearList {
public:
    virtual ~linearList() {};

    virtual bool empty() const = 0;

    virtual int size() const = 0;

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

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

    virtual void erase(int theIndex) = 0;

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

    virtual void Output(std::ostream &out) const = 0;

};

template<class T>
class arrayList : public linearList<T> {
public:
    arrayList(int initialCapacity = 10);

    arrayList(const arrayList<T> &);

    ~arrayList() { delete[]element; }


    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(std::ostream &out) const;

    class iterator {
    public:
        iterator(T *thePosition = 0) { position = thePosition; }

        T &operator*() const { return *position; }

        T *operator->() const { return &*position; }

        iterator &operator++() {
            ++position;
            return *this;
        }

        iterator operator++(int) {
            iterator old = *this;
            ++position;
            return old;
        }

        iterator &operator--() {
            --position;
            return *this;
        }

        iterator &operator--(int) {
            iterator old = *this;
            --position;
            return old;
        }

        bool operator!=(const iterator right) const {
            return position != right.position;
        }

        bool operator==(const iterator right) const {
            return position == right.position;
        }

    protected:
        T *position;
    };

    iterator begin() { return iterator(element); }

    iterator end() { return iterator(element + listSize); }

protected:
    void checkIndex(int theIndex) const;

    T *element;
    int arrayLenth;
    int listSize;
};

template<class T>
arrayList<T>::arrayList(int initialCapacity) {
    if (initialCapacity < 1) {
        throw "error";
    }
    arrayLenth = initialCapacity;
    element = new T[arrayLenth];
    listSize = 0;
}

template<class T>
arrayList<T>::arrayList(const arrayList<T> &theList) {
    arrayLenth = theList.arrayLenth;
    listSize = theList.listSize;
    element = new T[arrayLenth];
    copy(theList.element, theList.element + listSize, element);
}

template<class T>
void arrayList<T>::checkIndex(int theIndex) const {
    if (theIndex < 0 || theIndex >= listSize) {
        throw "error";
    }
}

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) (std::find(element, element + listSize, theElement) - element);
    if (theIndex == listSize)return -1;
    return theIndex;
}

template<class T>
void arrayList<T>::erase(int theIndex) {
    checkIndex(theIndex);
    std::copy(element + theIndex + 1, element + listSize, element + theIndex);
    element[--listSize].~T();
}

template<class T>
void arrayList<T>:: insert(int theIndex, const T &theElement) {
    if (theIndex < 0 || theIndex > listSize) {
        throw "error";
    }
    if (listSize == arrayLenth) {
        changeLength1D(element, arrayLenth, 2 * arrayLenth);
        arrayLenth *= 2;
    }
    for (int i = listSize - 1; i >= theIndex; --i) {
        element[1 + i] = element[i];
    }
    element[theIndex] = theElement;
    ++listSize;
}

template <class T>
void arrayList<T>::Output(std::ostream &out) const {
    for (int i = 0; i < listSize; ++i)
        out << element[i] << ' ';
}

template <class T>
std::ostream& operator<< (std::ostream& out,const arrayList<T>& x) {
    x.Output(out);
    return out;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值