数据结构C++—— 线性表与矩阵

本文详细介绍了线性表的概念、数组和链式描述,包括线性表的ADT、数组描述的优缺点以及链表的单向、循环和双向链表形式。同时,讨论了矩阵的定义、二维数组描述及其操作,还提及了特殊矩阵的压缩存储方法。
摘要由CSDN通过智能技术生成

一、线性表

1.1 线性表的定义

线性表(linear list):它的每一个实例都是元素的有序集合,其实例形式为: ( e 0 , e 1 , … , e n − 1 ) (e_0,e_1,…,e_{n-1}) (e0,e1,,en1),其中n 是有穷自然数。

线性表 の 常见操作

  • 创建线性表
  • 撤销线性表
  • 判断是否为空
  • 确定长度
  • 根据索引查找元素
  • 根据元素确定索引
  • 根据索引插入、删除元素
  • 按某种顺序打印线性表元素
  • ……

1.2 线性表的ADT

抽象数据类型

抽象数据类型LinearList
{
   实例
         有限个元素的有序集合,实例形式为:(e0,e1,…,en-1)
   操作
         empty():  如果表为空则返回true,否则返回false
         size():  返回表的大小(即表中元素个数)
         get(index):  返回表中索引为index的元素
         indexOf(x): 返回表中第一次出现的x的索引;如果x不在表中,则返回-1
         erase(index): 删除表中索引为index的元素,索引大于index的元素其索引值减1
         insert(index, x): 把x插入到表中索引为index的位置上,索引大于等于index的元素其索引值加1
         Output(out):  从左到右输出表中元素
}

对应的抽象类LinearList:

template <class T>
class linearList{
   
	public:
		virtual ~linearList(){
   };
		// 表空返回true,否则返回false
		virtual bool empty() const =0;
		// 返回表中元素个数
		virtual int size() const = 0 ;
		// 返回索引为index的元素
		virtual T& get(int index) const = 0;
		// 返回ele第一次出现的索引,若无则返回-1
		virtual int find(const T& ele) const = 0;
		// 删除索引为index的元素,其后所有元素索引-1
		virtual void erase(int index) const = 0;
		// 插入元素ele,后续元素索引+1
		virtual void insert(int index, T& ele) = 0;
		// 从左到右插入表中元素到输出流out
		virtual void Output(out) const = 0;
}

二、线性表的数组描述

2.1 数组描述:

  • 也叫公式化描述 / 顺序存储
  • 使用数组来存储线性表对象的每个实例
  • 元素在数组中的位置用一个数学公式来指明——>映射公式

2.2 几种典型的映射公式

  1. l o c a t i o n ( i ) = i location(i) = i location(i)=i,第i个元素位于数组第i个位置在这里插入图片描述
  2. l o c a t i o n ( i ) = a r r a y L e n g t h − i − 1 location(i) = arrayLength - i - 1 location(i)=arrayLengthi1,即从数组的末尾倒序存放元素在这里插入图片描述
  3. l o c a t i o n ( i ) = ( l o c a t i o n ( 0 ) + i ) % a r r a y L e n g t h location(i) = (location(0)+i) \% arrayLength location(i)=(location(0)+i)%arrayLength,将数组看成一个首尾相接的环,按照顺序依次存放在这里插入图片描述

2.3 数组描述的线性表——arrayList

template <class T>
 class arrayList : public linearList<T> 
{
    
	public:	
	//构造函数、复制构造函数、析构函数
	arrayList(int initialCapacity = 10); 
	arrayList(const arrayList<T>&); 
	~arrayList() {
   delete [] element;}
	//ADT方法
	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); 
	void Output(ostream& out) const;

	//其他方法
	int capacity() const {
   return arrayLength;}

	protected:	
     void checkIndex(int theIndex<
  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

边懵懵'.ㅅ'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值