• 数据结构C++,线性表的实现(数组方式)


    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <cmath>
    #include <iterator>
    #include <algorithm>

    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;
    };

    template<class T>
    void changeLength1D(T*& a, int oldLength, int newLength) {
      if(newLength<0) {
        std::cout<<"new length must be >=0"<<std::endl;

        exit(-1);

      }

      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 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);

      int capacity() const {
        return arrayLength;
      }
    protected:
      void checkIndex(int theIndex) const;

      T* element;
      int arrayLength;
      int listSize;
    };

    template<class T>
    arrayList<T>::arrayList(int initialCapacity) {
      if(initialCapacity<1) {
        std::ostringstream s;
        s<<"Initial Capacity = "<<initialCapacity<<" Must be > 0 ";
        exit(-1);
      }

      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];
      std::copy(theList.element,theList.element+listSize,element);
    }

    template<class T>
    void arrayList<T>::checkIndex(int theIndex) const {
      if(theIndex<0 || theIndex>=listSize) {
        std::ostringstream s;
        s<<"index = "<<theIndex<<" size = "<<listSize;
        exit(-1);
      }
    }

    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;
      else
        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) {
        std::ostringstream s;
        s<<"index = "<<theIndex<<" size = "<<listSize;
      }

      if(listSize == arrayLength) {
        changeLength1D(element,arrayLength,2*arrayLength);
        arrayLength*=2;
      }

      std::copy_backward(element+theIndex,element+listSize,element+listSize+1);

      element[theIndex]=theElement;

      listSize++;
    }

    int main() {
      arrayList<int> z;

      for(size_t i=0;i<30;++i) {
        z.insert(0,i);
      }

      for(size_t i=0;i<z.size();++i) {
        std::cout<<z.get(i)<<" ";
      }

      return 0;
    }

  • 相关阅读:
    Effective Go(官方文档)笔记
    Python中的列表、元祖、字典
    obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用
    HDU 3435A new Graph Game(网络流之最小费用流)
    蜗牛—Android基础之button监听器
    网络流小结
    Android 音频的播放之二MediaPlayer
    零基础学python-3.7 还有一个程序 python读写文本
    BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office的JavaScript对象模型
    jdk的动态代理源代码解析
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/8858531.html
Copyright © 2020-2023  润新知