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

该博客介绍了C++实现线性表的初始化、清空、判断空、获取长度、查找元素、遍历、插入和删除等基本操作。通过类模板定义了一个MyList类,包含各种成员函数用于线性表的管理。示例中展示了如何创建、插入和查找元素的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string>

using namespace std;

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

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

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

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

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

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

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

template <class T>
bool MyList<T>::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 <class T>
bool MyList<T>::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 <class T>
void  MyList<T>::ListTraverse()
{
	for (int i = 0; i < this->length; i++)
	{
		cout << this->elem[i] << "\t";
	}
	cout << endl;
}

template <class T>
bool MyList<T>::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 <class T>
bool MyList<T>::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 <class T>
MyList<T>::~MyList()
{
	if (elem)
	{
		delete[]elem;
		elem = NULL;
	}
}

int main()
{
	MyList<int> 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 <<boolalpha<< r2 << endl; L.ListTraverse();//删除第二个位置上的元素
	int key = 0;
	int key_next, key_prior;
	L.ListInsert(1, key);
	L.ListTraverse();
	bool r3=L.NextElem(key, key_next);
	bool r4 = L.PriorElem(key_next,key_prior);
	cout << "0的后继元素是:" << key_next << endl;
	cout << "1的前驱元素是:" << key_prior << endl;

	

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值