(1小时数据结构)数据结构c++描述(三)--- 线性表间接寻址

线性表间接寻址

目录

类定义:

构造函数与析构函数 :

 查找Find(int k, T & x):

寻找  Search(const T & x):

插入 Insert(int k, const T & x):

输出函数:

测试函数:

测试结果:

声明代码 


来看下间接寻址的基本概念吧:

       间接寻址(indirect addressing)是公式化描述和链表描述的组合。采用这种描述方法,可以保留公式化描述方法的许多优点——可以根据索引在 ( 1 )的时间内访问每个元素、可采用二叉搜索方法在对数时间内对一个有序表进行搜索等等。与此同时,也可以获得链表描述方法的重要特色——在诸如插入和删除操作期间不必对元素进行实际的移动。因此,大多数间接寻址链表操作的时间复杂性都与元素的总数无关.
       基本理论图:

类定义:

/*
数组结构中的线性表样式 间接寻址

对应书中代码:数据结构算法与应用c++描述

程序编写:比卡丘不皮

编写时间:2020年7月6日 10:45:02
*/
#pragma once

#include <iostream>
#include "all_error.h"

using namespace std;

template<class T>
class indirectList
{
public:
	indirectList(int MaxListSize = 10);
	~indirectList();
	bool isEmpty()const { return length == 0; }
	int Length() const { return length; }
	bool Find(int k, T &x) const;         //查找第k可的数据,没有为false
	int Search(const T & x) const;        //查找数据函数
	indirectList<T> & Delete(int k, T & x); //删除函数
	indirectList<T> & Insert(int k, const T& x); //插入函数
	void Output(ostream & out)const;
private:
	T **table;
	int length, MaxSize;
};

构造函数与析构函数 :

template<class T>
 indirectList<T>::indirectList(int MaxListSize)
{     //构造函数
	 MaxSize = MaxListSize;
	 table = new T *[MaxSize];
	 length = 0;
}

template<class T>
indirectList<T>::~indirectList()
{
	//析构函数
    for (int i = 0; i < length; i++)
    {
	   delete table[i];
    }
	delete[] table;
}

 查找Find(int k, T & x):

template<class T>
bool indirectList<T>::Find(int k, T & x) const
{
	if (k < 1 || k>length)
	{
		return false;
	}
	x = *table[k - 1];
	return true;
}

寻找  Search(const T & x):

template<class T>
int indirectList<T>::Search(const T & x) const
{
	for (int i = 1; i < length; i++)
	{
		if (*table[i-1] == x)
		{
			return i;
		}
	}
	return 0;
}

插入 Insert(int k, const T & x):

indirectList<T>& indirectList<T>::Insert(int k, const T & x)
{
	if (k<0 || k > length)
	{
		throw OutOfBounds();
	}
	if (length == MaxSize)
	{
		throw Noman();
	}

	//向后移动一位
	for (int i = length - 1; i >= k; i--)
	{
		table[i + 1] = table[i];
	}
	table[k] = new T;
	*table[k] = x;
	length++;
	return *this;
}

输出函数:

template<class T>
void indirectList<T>::Output(ostream & out) const
{
	for ( int i = 0; i<length; i++)
	{
		out << *table[i] << " ";
	}
}

//重载 <<

template<class T>
ostream & operator<<(ostream & out, const indirectList<T> & x)
{
	x.Output(out);
	return out;
}

测试函数:

void testindirectList()
{
	indirectList<int> indert;
	cout << "测试构造函数对应的数据" << endl;
	cout << "长度: " << indert.Length() << endl;
	cout << "链表数据: " << indert << endl;
	cout << endl;
	cout << "测试插入函数: 插入数据 Insert(1,1).Insert(2,2).Insert(3,3):"
		<< indert.Insert(0, 1).Insert(1, 2).Insert(2, 3) << endl;
	cout << "Insert(1, 6) " << indert.Insert(1, 6);
	cout << endl;
	//测试删除函数
	int x;
	indert.Delete(1, x);
	cout << "Delete(1, x): " << x << endl;
	cout << "链表数据: " << indert << endl;
	cout << endl;
	//find 
	indert.Find(2, x);
	cout << "链表数据: " << indert << endl;
	cout << "Find(2, x): " << x << endl;
	//测试Search函数

	cout << "Search(2) " << indert.Search(2) << endl;
		 
}

测试结果:

 

声明代码 

本章例子代码为《数据结构与算法 c++描述 第3版》中的间接寻址代码,这里没有添加一些代码。

喜欢的朋友可以关注我,一起进步。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值