[C++]使用vector描述线性表定义及基本操作
#ifndef VECTORLIST_H
#define VECTORLIST_H
#include<iostream>
#include"linearlist.h"
#include<vector>
#include<myexceptions.h>
using namespace std; template<class T>
class vectorList : public linearList<T>
{
public:
//构造函数
vectorList(int initialCapacity = 0); //复制构造函数
vectorList(const vectorList<T>&); //析构函数
~vectorList()
{
delete element;
} /*
* 类linearList中抽象方法的实现
*/
//判断是否表空
bool empty() const
{
return element->empty();
} //返回表内元素个数
int size() const
{
return (int)element->size();
} //返回索引为theIndex的元素
T& get(int theIndex) const; //返回元素theElement的索引
int indexOf(const T &theElement) const; //删除索引为theIndex的元素
void erase(int theIndex); //在索引为theIndex的位置插入元素theElement
void insert(int theIndex, const T &theElement); //将线性表元素放入输出流
void output(ostream &out) const; /*
* 增加的方法
*/
int capacity() const
{
return (int)element->capacity();
} /*
* 线性表的起始和结束位置的迭代器
*/
typedef typename vector<T>::iterator iterator;
iterator begin(){return element->begin();}
iterator end(){return element->end();}
protected:
void checkIndex(int theIndex) const;
vector<T>* element;//存储线性表元素的向量
}; /*
* 构造函数和复制构造函数的实现
*/
template<class T>
vectorList<T>::vectorList(int initialCapacity)
{
//构造函数
if(initialCapacity < 1)
{
ostringstream s;
s<<"Initial capacity = "<<initialCapacity<<"Must be > 0";
throw illegalParameterValue(s.str);
}
element = new vector<T>;//创建向量为0的空向量
element->reserve(initialCapacity);//vector的容量从0增加到initialCapacity } template<class T>
vectorList<T>::vectorList(const vectorList<T> &theList)
{
//复制构造函数
element = new vector<T>(*theList.element);
} //删除元素
template<class T>
void vectorList<T>::erase(int theIndex)
{
checkIndex(theIndex);
element->erase(begin()+theIndex);
} //插入元素
template<class T>
void vectorList<T>::insert(int theIndex, const T &theElement)
{
if(theIndex < 0 || theIndex > size())
{
//无效索引
ostringstream s;
s<<"index = "<<theIndex <<" size = "<<size();
throw illegalIndex(s.str());
}
element->insert(element->begin()+theIndex,theElement);
} #endif // VECTORLIST_H
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H #include <string> using namespace std; // illegal parameter value
class illegalParameterValue
{
public:
illegalParameterValue(string theMessage = "Illegal parameter value")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // illegal input data
class illegalInputData
{
public:
illegalInputData(string theMessage = "Illegal data input")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // illegal index
class illegalIndex
{
public:
illegalIndex(string theMessage = "Illegal index")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // matrix index out of bounds
class matrixIndexOutOfBounds
{
public:
matrixIndexOutOfBounds
(string theMessage = "Matrix index out of bounds")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // matrix size mismatch
class matrixSizeMismatch
{
public:
matrixSizeMismatch(string theMessage =
"The size of the two matrics doesn't match")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // stack is empty
class stackEmpty
{
public:
stackEmpty(string theMessage =
"Invalid operation on empty stack")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // queue is empty
class queueEmpty
{
public:
queueEmpty(string theMessage =
"Invalid operation on empty queue")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // hash table is full
class hashTableFull
{
public:
hashTableFull(string theMessage =
"The hash table is full")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // edge weight undefined
class undefinedEdgeWeight
{
public:
undefinedEdgeWeight(string theMessage =
"No edge weights defined")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // method undefined
class undefinedMethod
{
public:
undefinedMethod(string theMessage =
"This method is undefined")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
};
#endif // MYEXCEPTIONS_H
#ifndef LINEARLIST_H
#define LINEARLIST_H // LINEARLIST_H
// abstract class linearList
// abstract data type specification for linear list data structure
// all methods are pure virtual functions #include <iostream> using namespace std; template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
// return true iff list is empty
virtual int size() const = 0;
// return number of elements in list
virtual T& get(int theIndex) const = 0;
// return element whose index is theIndex
virtual int indexOf(const T& theElement) const = 0;
// return index of first occurence of theElement
virtual void erase(int theIndex) = 0;
// remove the element whose index is theIndex
virtual void insert(int theIndex, const T& theElement) = 0;
// insert theElement so that its index is theIndex
virtual void output(ostream& out) const = 0;
// insert list into stream out
};
#endif
[C++]使用vector描述线性表定义及基本操作的更多相关文章
- 模仿std::vector写线性表的几点感想
数据结构还是很早之前学的了,当时才刚学过C语言,实现得都很简单,最近决定重新打牢基础,于是重新开始实现书上的数据结构和算法. 模仿C++ Primer的StrVec以及std::vector,使用模板 ...
- 《数据结构与STL-第二章 线性表》读书笔记
线性表 定义 线性表(linear list)是由零个或多个相同类型的数据元素构成的有限序列. 存储结构 顺序存储 最简单的存储方法是顺序存储法,即把线性表的数据元素按照逻辑次序顺序地放在一组地址连续 ...
- 动态分配的顺序线性表的十五种操作—C语言实现
线性表 定义:是最常用的,也是最简单的数据结构,是长度为n个数据元素的有序的序列. 含有大量记录的线性表叫文件 记录:稍微复杂的线性表里,数据元素为若干个数据项组成,这时把一个数据元素叫记录 结构特点 ...
- 线性表之顺序存储结构(C语言动态数组实现)
线性表的定义:N个数据元素的有限序列 线性表从存储结构上分为:顺序存储结构(数组)和 链式存储结构(链表) 顺序存储结构:是用一段连续的内存空间存储表中的数据 L=(a1,a2,a3....an) 链 ...
- 数据结构与算法(C/C++版)【绪论/线性表】
声明:数据结构与算法系列博文参考了<天勤高分笔记>.<王道复习指导>.C语言中文网.非商业用途,仅为学习笔记总结! 第一章<绪论> 一.基本概念及入门常识 /// ...
- TOJ 1214: 数据结构练习题――线性表操作
描述 请你定义一个线性表,可以对表进行"在某个位置之前插入一个元素"."删除某个位置的元素"."清除所有元素"."获取某个位置的元 ...
- 线性表&顺序线性表
第二章 线性表 参考文献:[数据结构(C语言版)].严蔚敏 本篇章仅为个人学习数据结构的笔记,不做任何用途. 2.1 线性结构的特点 (1). 存在唯一的一个被称为"第一个"的数据 ...
- c语言数据结构学习心得——线性表
线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
随机推荐
- Flume(四)【配置文件总结】
目录 一.Agent 二.Source taildir arvo netstat exec spooldir 三.Sink hdfs kafka(待续) hbase(待续) arvo logger 本 ...
- Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)
从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...
- 【编程思想】【设计模式】【其他模式】graph_search
Python版 https://github.com/faif/python-patterns/blob/master/other/graph_search.py #!/usr/bin/env pyt ...
- easyhadoop 安装
ldconfig deferred processing now taking place正在处理用于 libapache2-mod-php5 的触发器... * Reloading web serv ...
- 编译安装haproxy2.0
先解决lua环境,(因为centos自带的版本不符合haproxy要求的最低版本(5.3)先安装Lua依赖的包 [root@slave-master lua-5.3.5]# yum install ...
- SpringBoot的定时任务
springBoot定时任务可分为多线程和单线程,而单线程又分为注解形式,接口形式 1.基于注解形式 基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影 ...
- sftp 上传下载 命令介绍
sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...
- 注册页面css版本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Wireshark(五):TCP窗口与拥塞处理
原文出处: EMC中文支持论坛 TCP通过滑动窗口机制检测丢包,并在丢包发生时调整数据传输速率.滑动窗口机制利用数据接收端的接收窗口来控制数据流. 接收窗口值由数据接收端指定,以字节数形式存储于TCP ...
- jdk1.8安装教程
JDK1.8安装包下载 链接:https://pan.baidu.com/s/18pEMo3gYsAAHWC9DjizP1A 提取码:xu99 1.双击JDK1.8的安装包,并点击下一步 2.选择安装 ...