数据结构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版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
随机推荐
- mysql常用命令介绍
mysql适用于在Internet上存取数据,支持多种平台 1.主键:唯一标识表中每行的这个列,没有主键更新或删除表中的特定行很困难. 2.连接mysql可以用Navicat 要读取数据库中的内容先要 ...
- Html test
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- C# 字符串到字节数组,字节数组转整型
; , ); byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 num = BitConverter.ToInt32(bytes, ) ...
- get传数组
开发真的处处都是坑呀 ajax get请求,传数组,想当然的给了个json数组['','',''],结果500错误 正确的方式,多次赋值,见下图,后台会自动转数组
- Swift 3到5.1新特性整理
本文转载自:https://hicc.me/whats-new-in-swift-3-to-5-1/,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有. Hipo 2.0 重写从 Swif ...
- CAD在网页绘一个直线,得到直线id,再调该得到直线对象,然写扩展数据
IMxDrawDatabase::ObjectIdToObject 实体id返回实体对象. 参数 说明 [in] LONGLONG lId 实体id JS代码,中绘一个直线,得到直线id,再调该得到直 ...
- git 缓存密码导致的不能和远程仓库交互unable to access... 403错误
尝试了各种方式,包括卸载等最终解决方案: 查看本机的credential 是否已经被清空. 如果输入了 git config credential.helper 命令之后没有输出,说明 git 的配置 ...
- join 和 left join 和 right join的区别?
join等价于 inner join 是内连接 ,返回两个表都有的符合条件的行. left join 是左连接,返回坐表中所有的行以及右表中符合条件的行. right join右连接,是返回右表中所有 ...
- 34.分组聚合操作—bucket
主要知识点: 学习聚合知识 一.准备数据 1.家电卖场案例背景建立index 以一个家电卖场中的电视销售数据为背景,来对各种品牌,各种颜色的电视的销量和销售额,进行各种各样角度的分析 ...
- 20170704-WNDR4300串口连接信息
find_hif: bootstrap = 0xaf055aWASP BootROM Ver. 1.1Nand Flash initONFI: Control setting = 0xb44find_ ...