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

数据结构C++,线性表的实现的更多相关文章

  1. 用C#学习数据结构之线性表

    什么是线性表 线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系.这种一对一的关系指的是数据元素之间的位置关 ...

  2. 数据结构之线性表(python版)

    数据结构之线性表(python版) 单链表 1.1  定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...

  3. C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...

  4. javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现

    线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...

  5. javascript实现数据结构:线性表--线性链表(链式存储结构)

    上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...

  6. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  7. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  8. C 数据结构1——线性表分析(顺序存储、链式存储)

    之前是由于学校工作室招新,跟着大伙工作室招新训练营学习数据结构,那个时候,纯碎是小白(至少比现在白很多)那个时候,学习数据结构,真的是一脸茫然,虽然写出来了,但真的不知道在干嘛.调试过程中,各种bug ...

  9. 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)

    温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...

  10. 【Java】 大话数据结构(1) 线性表之顺序存储结构

     本文根据<大话数据结构>一书,实现了Java版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...

随机推荐

  1. lamlmzhang的新博客开通了,欢迎大家的关注

    从这里开始lamlmzhang的java开发之路~!

  2. Python 递归、匿名函数、map和filter day4

    一.递归---函数自己调用自己 1.一个错误递归的例子: count=0 def hello(): global count count+=1 print("count %s"%c ...

  3. 1 JSONP

    一.什么是跨域访问举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请求 ...

  4. jquery spa

    1.hashchange监听 2.根据url加载不同页面 $.ajax({ url:"/xx/xx.html" type:"get", dataType:&qu ...

  5. Opencv学习之路——自己编写的HOG算法

    #include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv ...

  6. DP——最长上升子序列(LIS)

    DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...

  7. NSE入门--nmap 脚本基础

  8. RHEL6 配置Yum库

    在/mnt目录下创建子目录“/cdrom”(用于将iso文件挂载到此目录下) 镜像状态确定为“已连接”(“已连接”未勾选的情况下无法获得iso文件) 配置自动挂载文件 (系统开机时会主动读取“/etc ...

  9. 解决WP程序 重复打开出现 “正在加载...” 字样 解决方案

    在开发winphone程序时候 我们经常遇到调试.在调试的时候 可能会重复打开 debug一下.可是有时候 经常遇到 "正在加载...."字样.而且很慢.效率很低. 测试发现 在 ...

  10. Django——3 模板路径 模板变量 常用过滤器 静态文件的使用

    Django 模板路径 模板变量 过滤器 静态文件的加载 模板的路径,有两种方法来使用 设置一个总的templates在大项目外面,然后在sittings的TEMPLATES中声明 在每一个APP中创 ...