数据结构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版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
随机推荐
- 【sqli-labs】 less50 GET -Error based -Order By Clause -numeric -Stacked injection(GET型基于错误的整型Order By从句堆叠注入)
报错没有关闭,直接可以用UpdateXml函数 http://192.168.136.128/sqli-labs-master/Less-50/?sort=1 and UpdateXml(1,conc ...
- Centos6.7 安装Naigos教程
Centos6.7 安装Naigos教程参考文档:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickst ...
- STL_string用法总结
参考自:http://blog.csdn.net/y990041769/article/details/8763366 1:string对象的定义和初始化以及读写 string s1; 默认 ...
- HDU_3308_线段树_区间合并
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Redis 之消息发布与订阅(publish、subscribe)
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 一般做群聊,聊天室,发布公告信息等.
- acedinitget
// 提示用户选择选择方式 acedInitGet(0, _T("W CP")); int nRs = acedGetKword(_T("\n请输入关键字确定选择方式[窗 ...
- Java数值数学类
Java数值数学类 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo() 将number对象与参数比较. 3 equals ...
- DOCKER - POD操作
强制删除 Terminating 的pod kubectl delete -n <namespace> <pod podname> --grace-period=0 --fo ...
- 通过JS唤醒app(安卓+ios)
有需求说要通过页面按钮唤醒app,或者手机上没有这款app跳转到商店,然后刚开始也是查了资料的,结果发现一头雾水,不过最后还是捣鼓出来了,当然也参考了前人分享的经验,下面我就将方法整理一下: 首先明确 ...
- redis 和 memcached的区别
redis和memcached的区别 Redis 和 Memcache 都是基于内存的数据存储系统.Memcached是高性能分布式内存缓存服务:Redis是一个开源的key-value存储系统. ...