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

线性表顺序表示的C++语言实现

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

线性表顺序表示的C++语言实现

#include
#include

using namespace std;

const int Maxsize = 20;
const int ERROR = 0;
const int OK = 1;

template 
class MyList
{
public:
	MyList();//  初始化
	~MyList();
	void InitList(int i,T *e);
	void ClearList();                                   // 将数组长度设为0
	bool ListEmpty();                                   // 判断数组是否为空
	int ListLength();                                   // 获取数组长度
	bool GetElem(int i, T& e);                       // 查找指定下标元素,如果找到返回true,同时返回第i个数据元素的值
	int LocateElem(T e);                            // 查找指定元素,并返回其序号
	bool PriorElem(T& currentElem, T& preElem);   // 根据输入的元素,查找元素的前驱元素,如果找到返回true,同时返回前驱元素
	bool NextElem(T& currentElem, T& nextElem);   // 根据输入的元素,查找元素的后继元素,如果找到返回true,同时返回后继元素
	void ListTraverse();                                // 遍历线性表,输出元素
	bool ListInsert(int i, T& e);                    // 在指定位置插入一个元素
	bool ListDelete(int i);                    // 删除指定位置元素


private:
	T* elem;
	int length;
};

template //初始化类模板的格式
MyList::MyList()
{
	this->length = 0;
	this->elem = new T[Maxsize];//开辟了一段连续的数组空间,delete的时候,要用delete[]
	if (!elem)
		exit(OVERFLOW);
}
template 
void MyList::InitList(int i,T *e)
{
	cout << "开始进行列表初始化!" << endl;
	for (int i = 0; i < 5; i++)
	{
		this->elem[i] = *e;
		e++;
		this->length++;
	}
}

template 
void MyList::ClearList()
{
	this->length = 0;
}

template 
bool MyList::ListEmpty()
{
	if (!this->length)
		return true;
	else
		return false;
}

template 
int MyList::ListLength()
{
	return this->length;
}

template 
bool MyList::GetElem(int i, T& e)
{
	if (i > this->length || i < 1)
		return false;
	else
	{
		e = this->elem[i - 1];
		return true;
	}
}

template 
int  MyList::LocateElem(T e)
{
	for (int i = 0; i < this->length; i++)
	{
		if (this->elem[i] == e)
			return i + 1;//返回的是序号,而不是在内存中的序号
	}
	return ERROR;
}

template 
bool MyList::PriorElem(T& currentElem, T& preElem)
{
	int temp = this->LocateElem(currentElem);
	if (temp == 1)
	{
		cout << "第一个元素无前驱元素" << endl;
		return false;
	}
	else
	{
		preElem = this->elem[temp - 2];
		return true;
	}
}

template 
bool MyList::NextElem(T& currentElem, T& nextElem)
{
	int temp = this->LocateElem(currentElem);
	if (temp == this->length)
	{
		cout << "最后一个元素无前驱元素" << endl;
		return false;
	}
	else
	{
		nextElem = this->elem[temp];
		return true;
	}
}

template 
void  MyList::ListTraverse()
{
	for (int i = 0; i < this->length; i++)
	{
		cout << this->elem[i] << "t";
	}
	cout << endl;
}

template 
bool MyList::ListInsert(int i, T& e)
{
	//判断输入是否合法
	if (i<0 || i>this->length)
	{
		cout << "插入位置有误!" << endl;
		return false;
	}
	//判断存储空间是否已满
	if (this->length == Maxsize)
	{
		cout << "存储空间已满" << endl;
		return false;
	}
	for (int j = this->length - 1; j >= i - 1; j--)
	{
		this->elem[j + 1] = this->elem[j];
	}
	this->elem[i - 1] = e;
	++this->length;
	return true;
}

template 
bool MyList::ListDelete(int i)
{
	//判断输入是否合法
	if (i<0 || i>this->length)
	{
		cout << "插入位置有误!" << endl;
		return false;
	}
	//判断是否有元素可删
	if (!this->length)
	{
		cout << "线性表中无元素可删" << endl;
		return false;
	}
	for (int j = i - 1; j < this->length; j++)
	{
		this->elem[j] = this->elem[j + 1];
	}
	this->length--;
	return true;
}

template 
MyList::~MyList()
{
	if (elem)
	{
		delete[]elem;
		elem = NULL;
	}
}

int main()
{
	MyList L;
	int Arry[5] = { 1,2,3,4,5};
	L.InitList(5,Arry);
	L.ListTraverse();
	//int r1 = L.ListLength(); cout << r1 << endl;//获取元素个数
	//bool r2 = L.ListDelete(2); cout < 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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