顺序表模板类

#pragma once

const int size = 50;

template <typename T>
class Nodelist
{
private:
	T * elem;
	int length;
public:
	Nodelist();//构造空表
	~Nodelist() = default;//销毁空表
	Nodelist(const Nodelist&);//拷贝构造函数
	bool clearList();//重置为空表
	bool isEmpty();//是否为空表
	int listLength();//返回表长
	bool getElem(T&, const int);//取得特定位置的元素
	int locateElem(const int);//返回第一个相同的元素
							  //void priorElem();返回特定元素的前驱
							  //void nextElem();返回特定元素的后驱     这两个没什么鸟用
	bool listInsert(const T, const int);//插入
	bool listDelete(T&, const int);//删除
	void allOut();//打印表中所有元素
	bool push(const T);//后插
	bool pop(T&);//后取
	bool pop();
	void listSort();//垃圾冒泡排序

	template <typename T>
	friend const Nodelist<T> operator+(Nodelist<T>&, Nodelist<T>&);
	template <typename T>
	friend const Nodelist<T> orderlyMerge(Nodelist<T>&, Nodelist<T>&);
};

template <typename T>
inline Nodelist<T>::Nodelist()
{
	elem = new T[size];
	length = 0;
	std::cout << "the list has been built!" << std::endl;
}

template <typename T>
inline Nodelist<T>::Nodelist(const Nodelist<T> &h)
{
	Nodelist<T> *p = new Nodelist<T>;
	p->elem = h.elem;
	p->length = h.length;
	elem = p->elem;
	length = p->length;
}

template <typename T>
bool Nodelist<T>::clearList()
{
	if (!elem) {
		std::cout << "the list is not exit" << std::endl;
		return false;
	}
	delete elem;
	length = 0;
	return true;
}

template <typename T>
bool Nodelist<T>::isEmpty()
{
	if (length)
		return false;
	else
		return true;
}

template <typename T>
int Nodelist<T>::listLength()
{
	return length;
}

template <typename T>
bool Nodelist<T>::getElem(T &ref, const int loc)
{
	if (loc > length + 1 || loc < 1) {
		std::cout << "the element's location is illegal" << std::endl;
		return false;
	}
	ref = elem[loc - 1];
	return true;
}

template <typename T>
int Nodelist<T>::locateElem(const int ref)
{
	bool loc = 0;
	for (int i = 0; i != length; i++)
	{
		if (ref == elem[i]) {
			loc = i + 1;
			break;
		}
	}
	if (loc == 0) {
		std::cout << "there is no suitable value" << std::endl;
	}
	return loc;
}

template <typename T>
bool Nodelist<T>::listInsert(const T ref, const int loc)
{
	if (loc > length + 1 || loc < 1) {
		std::cout << "you cannot insert that in this position" << std::endl;
		return false;
	}
	else if (length == size - 1) {
		std::cout << "the list is full" << std::endl;
		return false;
	}
	for (int j = length - 1; j > loc - 1; j--) {
		elem[j + 1] = elem[j];
	}
	elem[loc - 1] = ref;
	length++;
	return true;
}

template <typename T>
bool Nodelist<T>::listDelete(T &ref, const int loc)
{
	if (loc > length + 1 || loc < 1) {
		std::cout << "you cannot delete that in this position" << std::endl;
		return false;
	}
	ref = elem[loc - 1];
	for (int j = loc; j < length; j++) {
		elem[j - 1] = elem[j];
	}
	length--;
	return true;
}

template <typename T>
void Nodelist<T>::allOut()
{
	for (int i = 0; i != length; i++) {
		std::cout << "the " << i + 1 << "st one: " << elem[i] << std::endl;
	}
}

template <typename T>
bool Nodelist<T>::push(const T ref)
{
	if (length == size - 1) {
		std::cout << "the list is full" << std::endl;
		return false;
	}
	elem[length] = ref;
	length++;
	return true;
}

template <typename T>
bool Nodelist<T>::pop(T &ref)
{
	if (length < 1) {
		std::cout << "the list is empty" << std::endl;
		return false;
	}
	ref = elem[length - 1];
	length--;
	return true;
}

template <typename T>
bool Nodelist<T>::pop()
{
	if (length < 1) {
		std::cout << "the list is empty" << std::endl;
		return false;
	}
	length--;
	return true;
}

template <typename T>
void Nodelist<T>::listSort()
{
	T t;
	bool flag = true;
	for (int i = 0; i < length && flag == true; i++) {
		flag = false;
		for (int j = 0; j < length - 1 - i; j++) {
			if (elem[j] > elem[j + 1]) {
				t = elem[j];
				elem[j] = elem[j + 1];
				elem[j + 1] = t;
				flag = true;
			}
		}
	}
}

template <typename T>
const Nodelist<T> operator+(Nodelist<T> &leftList, Nodelist<T> &rightList)
{
	if (leftList.length + rightList.length > size) {
		std::cout << "over size, you can not add them" << std::endl;
		return leftList;
	}
	Nodelist<T> ref(leftList);

	for (int i = ref.length; i < ref.length + rightList.length; i++) {
		ref.elem[i] = rightList.elem[i - ref.length];
	}
	ref.length += rightList.length;
	return ref;
}

template <typename T>
const Nodelist<T> orderlyMerge(Nodelist<T> &lhs, Nodelist<T> &rhs)
{
	Nodelist<T> ref;
	int loc = 1;
	ref.length = lhs.length + rhs.length;
	int i = 1, j = 1;
	int iL, iR;
	while (loc != ref.length + 1 && j != rhs.length + 1 && i != lhs.length + 1) {
		lhs.getElem(iL, i);
		rhs.getElem(iR, j);
		if (iL > iR) {
			ref.listInsert(iR, loc++);//由于这里表长加一,楼下要减一
			ref.length--;
			j++;
		}
		else {
			ref.listInsert(iL, loc++);
			ref.length--;
			i++;
		}
	}
	if (i != lhs.length + 1) {
		for (; i != lhs.length + 1; i++) {
			lhs.getElem(iL, i);
			ref.listInsert(iL, loc++);
			ref.length--;
		}
	}
	if (j != rhs.length + 1) {
		for (; j != rhs.length + 1; j++) {
			rhs.getElem(iR, j);
			ref.listInsert(iR, loc++);
			ref.length--;
		}
	}
	return ref;
}

终于实现了,不容易啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值