栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c/c++实现顺序表的顺序存储结构,数据结构

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c/c++实现顺序表的顺序存储结构,数据结构

数组结构之顺序表 define:

用一组地址连续的存储单元依次存储线性表元素,线性表的这种机内表示称作线性表的顺序存储结构,简称顺序表。

特点:

为表中相邻的元素Ai和Ai+1赋以相邻的存储位置LOC(Ai)和LOC(Ai+1)。换句话说,以元素在计算机内“物理位置相邻”来表示线性表中数据元素之间的逻辑关系。

上代码:
#include
using namespace std;
const  int SIZE = 100;
typedef int Type;
//构造一个线性表类
class List
{
public:
	List(Type size);
	~List();
	int Find(Type n);                    //查找元素,并返回下标
	bool Full();
	bool Empty();
	void Out();                          //打印表
	bool Insert(int index, Type m);      //指定位置插入元素
	bool Delete(int index);              //删除指定位置元素
	void GetListLength();
protected:
	Type* p;//基地址
	int Size;
	int Length;
};
//构造一个空表
List::List(Type size)
{
	this->Size = size;
	this->Length = 0;
	this->p = new Type[Size];//堆区申请内存
	if (!p)
	{
		cout << "内存分配失败" << endl;
		exit(-1);
	}
}
//析构
List::~List()
{
	delete[]p;
}
void List::GetListLength()
{
	cout << "表的长度为" << Length << endl;
}
//查找元素是否存在(返回下标)
int List::Find(Type n)
{
	if (!Empty())
	{
		for (int i = 0; i < Length; i++)
		{
			if (p[i] == n)//如果找到就返回元素下标+1
			{
				return i + 1;
			}
	    }
	}
	//元素不在表中返回-1;
	return -1;
}
bool List::Insert(int index, Type m)
{
	//判断插入位置是否合理
	if (index<1 || index>Length + 1||Full())
	{
		return false;
	}
	else
	{
		Length++;//表长度+1
		//从最后一个元素开始逐个往后移
		for (int i = Length - 1; i >= index - 1; --i)
		{
			p[i] = p[i - 1];
		}
		//插入
		p[index - 1] = m;
		return true;
	}
}
bool List::Delete(int index)
{
	//判断删除位置是否合理
	if (index<1 || index>Length + 1)
	{
		return false;
	}
	else
	{
		//从当前元素开始后继元素逐个前移
		for (int i = index-1; i 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号