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

最新文章

  1. Service Plugin / Agent - 每天5分钟玩转 OpenStack(73)
  2. 关于NotePad一些功能的实现方法
  3. Codeforces 722C. Destroying Array
  4. SQL Server Window Function 窗体函数读书笔记二 - A Detailed Look at Window Functions
  5. 关于OAUTH2.0的极品好文
  6. php curl简单使用
  7. HDU 1796How many integers can you find(简单容斥定理)
  8. SQL Prompt 破解之道
  9. 多种解法解决n皇后问题
  10. 文件系统及程序的限制关系: ulimit
  11. 缓存,减少对sql语句的访问
  12. python常用模块之时间模块
  13. idea项目左边栏只能看到文件看不到项目结构
  14. java基础知识(初学)
  15. oracle client PLSQL配置
  16. NIO之阻塞IO与非阻塞IO(包含Selector使用)
  17. 《图解HTTP》阅读笔记--第六章--HTTP首部
  18. Dijkstra单源最短路径,POJ(2387)
  19. 快速幂取模(当数很大时,相乘long long也会超出的解决办法)
  20. ctrip-apollo windows环境部署

热门文章

  1. servlet-请求重定向
  2. 【转】SSH中 整合spring和proxool 连接池
  3. CentOS下使用yum安装配置和使用svn
  4. 【源码阅读】opencv中opencl版本的dft函数的实现细节
  5. 使用Python的Flask框架,结合Highchart,动态渲染图表(Ajax 请求数据接口)
  6. 64.root object的理解
  7. 【3】数据筛选2 - requests
  8. sdp概览
  9. 4 pandas模块,Series类
  10. HTML-class与id的区别及应用