数据结构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;
}
数据结构C++,线性表的实现的更多相关文章
- 用C#学习数据结构之线性表
什么是线性表 线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系.这种一对一的关系指的是数据元素之间的位置关 ...
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...
- C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作
1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...
- javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现
线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...
- javascript实现数据结构:线性表--线性链表(链式存储结构)
上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...
- Java数据结构之线性表(2)
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- Java数据结构之线性表
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- C 数据结构1——线性表分析(顺序存储、链式存储)
之前是由于学校工作室招新,跟着大伙工作室招新训练营学习数据结构,那个时候,纯碎是小白(至少比现在白很多)那个时候,学习数据结构,真的是一脸茫然,虽然写出来了,但真的不知道在干嘛.调试过程中,各种bug ...
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 【Java】 大话数据结构(1) 线性表之顺序存储结构
本文根据<大话数据结构>一书,实现了Java版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
随机推荐
- Ajax——jq中ajax的使用
格式化表单 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 一个有趣的 ”Validation of viewstate MAC failed” 错误的发现和解决
在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户端 收到服务器对PostBack的响应后,仍然可以展现在PostBack之前设定 ...
- TensorFlow:Windows下使用TensorFlow-Python版本
原文链接:Win10X64下安装使用TensorFlow 安装TensorFlow 由于Google那帮人已经把 TensorFlow 打成了一个 pip 安装包,所以现在可以用正常安装包的方式安装 ...
- 非常好用的1款UI自动化测试工具:airTest
网易团队开发的UI自动化测试神器airTest,下载地址:http://airtest.netease.com/tutorial/Tutorial.html Appium和airTest对比,我的看法 ...
- 惊了!!! 小白零基础学java (月薪过万是你的梦想嘛) 手把手教学 就怕你不动手【二十五】第二章【初识MySQL】
初识MySQL1. 了解主流的数据库和数据库分类1.1 数据库概念数据库:按照数据结构来组织.存储和管理数据的一种建立在计算机存储设备上的仓库. 数据库的优势: 1. 可以持久化存储大量的数据.方便我 ...
- Uedior上传大文件超时报错
出错原因: 1.php超时等待时间太短 2.uedior中设置了请求超时,提示信息: 上传失败,请重试 先解决第一个问题: 设置php.ini中的max_execution_time 为0 (意思是h ...
- 一篇入门AngularJS
目录 1.AngularJS 应用 2.AngularJS 指令 3.AngularJS 表达式 4.AngularJS 模型 5.AngularJS 控制器 6.AngularJS 作用域 7.An ...
- 从零开始的 webpack4 + vue2.x
新建文件夹 webpack-vue 安装依赖 yarn init //初始化package.json yarn add webpack webpack-cli //添加webpack.webpack- ...
- vue项目中的常见问题(vue-cli版本3.0.0)
一.样式问题 1.vue中使用less 安装less依赖 npm install less less-loader --save-dev 在使用时 在style标签中加入 lang="les ...
- (蓝桥杯)第八届A组C/C++跳蚱蜢
#include<iostream> #include<memory.h> #include<stack> #include<string> #incl ...