动态顺序表C++实现

写成员函数的时候参考了下严蔚敏的数据结构(C语言版)

/*
顺序表的类模板
有以下功能:
												1.CreateSqlist用长度为n,容量为c,类型为T的数组a创建顺序表
												2.displaylist按顺序输出表中元素
												3.Sqempty检测表是否为空,为空返回1,否则返回0
												4.Sqlen输出并返回表的长度
												5.getelem返回下标为i的元素
												6.locElem从第一个元素开始,寻找并返回第一个与e满足谓词compare的元素的下标
												7.ListInsert在i位置插入类型为T的元素
												8.Delete删除下标为i的元素
												9.pushback尾插
												10.pushfront头插
												11.popback尾删
												12.popfront头删
												13.cheakV检查容量是否为满,为满则追加分配10个数据的容量,否则什么都不做
												14.compare(T a,T b)如果a<b则返回true,否则返回false;
作者:chczy
*/
#include<iostream>
#include<algorithm>
#include<new>
using namespace std;
#define MAXSIZE 1000
#define Realloc 10
//顺序表
template <class T>
class Sqlist {
public:
	bool CreateSqlist(T a[], int n, int c);
	void displaylist();
	bool Sqempty();
	int Sqlen();
	T getelem(int i);
	int locElem(T e,int compare(T a,T b));//取满足条件的元素
	void ListInsert(int i, T e);
	void Delete(int i);
	void pushback(T e);
	void pushfront(T e);
	void popback();
	void popfront();
	void cheakV();//检查顺序表容量是否满了
	bool compare(T e,T d);
	Sqlist();
	Sqlist(const Sqlist&);
	Sqlist& operator=(const Sqlist&);
	~Sqlist() { cout << "destory Sqlist" << endl; }
private:
	T *data;
	int len;
	int cap;
};

template <class T>
bool Sqlist<T>::compare(T e,T d)
{
	return e < d;
}//比较两T元素的大小

template <class T>
Sqlist<T>::Sqlist()//创建一个空表
{
	cap = MAXSIZE;
	data = new T[cap];
	len = 0;
}

template <class T>
bool Sqlist<T>::CreateSqlist(T a[], int n, int c)//传入一个T类型的数组以及数组长度和数组容量来创建线性表;
{
	if (Sqempty())
	{
		cap = c;
		len = n;
		T *now = new T[cap];
		for (int i = 0; i < len; ++i)
		{
			now[i] = a[i];
		}
		delete[] data;
		data = now;
		now = NULL;
		delete now;
		return 1;
	}
	else
		return 0;
}

template <class T>
void Sqlist<T>::displaylist()//打印顺序表中所有元素
{
	if (len == 0)
	{
		cout << "null list!" << endl;
		return;
	}
	cout << "The Sequence List's length is " << len << endl;
	cout << "It's elems is:";
	for (int i = 0; i < len; ++i)
		cout << data[i] << " ";
	cout << endl;
}

template <class T>
bool Sqlist<T>::Sqempty()//判断顺序表是否为空
{
	if (len == 0)
		return 1;
	else
		return 0;
}

template <class T>
int Sqlist<T>::Sqlen()//返回表的长度
{
	cout << "表长为:" << endl;
	cout << len << endl;
	return len;
}

template <class T>
T Sqlist<T>::getelem(int i)//取下标为i的元素
{
	return data[i];
}

template <class T>
int Sqlist<T>::locElem(T e,int compare(T a,T b))//寻找表中第一个满足compare条件额=的元素,compare自写
{
	int i;
	for (i = 0; i < len; ++i)
	{
		if (compare(e,data[i]))
		{
			return i;
		}
	}
	if (i >= len)
	{
		cout << "NOT THIS ELEM!" << endl;
		return 0;
	}
}

template <class T>
void Sqlist<T>::cheakV()//检查标的容量是否为满
{
	if (len >= cap)
	{
		cap += Realloc;
		T *now = new T[cap];
		for (int i = 0; i < len; ++i)
		{
			now[i] = data[i];
		}
		delete[] data;
		data = now;
		now = NULL;
	}
}

template <class T>
void Sqlist<T>::pushback(T e)//尾插
{
	cheakV();
	data[len] = e;
	++len;
}

template <class T>
void Sqlist<T>::pushfront(T e)//头插
{
	cheakV();
	for (int j = len; j > 0; --j)
	{
		data[j] = data[j - 1];
	}
	data[0] = e;
	++len;
}

template <class T>
void Sqlist<T>::popback()//尾删
{
	if (len == 0)
		throw"error,no elem!\n";
	else
	{
		--len;
		T *now = new T[cap];
		for (int i = 0; i < len; ++i)
		{
			now[i] = data[i];
		}
		delete[] data;
		data = now;
		now = NULL;
	}
}

template <class T>
void Sqlist<T>::popfront()//头删
{
	if (len == 0)
		throw"no elem!\n";
	else if (len == 1)
		popback();
	else
	{
		for (int j = 0; j < len - 1; ++j)
		{
			data[j] = data[j + 1];
		}
		--len;
		T *now = new T[cap];
		for (int i = 0; i < len; ++i)
		{
			now[i] = data[i];
		}
		delete[] data;
		data = now;
		now = NULL;
	}
}

template <class T>
void Sqlist<T>::ListInsert(int i, T e)//在i位置插入一元素
{
	if (i > len)
		throw"error,out of range\n";
	cheakV();
	if (i == len)
		pushback(e);
	else if (i == 0)
		pushfront(e);
	else
	{
		for (int j = len-1; j >= i; --j)
		{
			data[j+1] = data[j];
		}
		data[i] = e;
		++len;
	}
}

template <class T>
void Sqlist<T>::Delete(int i)//删除下标为i的元素
{
	if (i >= len)
		throw"error,over range\n";
	if (i == len - 1)
		popback();
	else if (i == 0)
		popfront();
	else
	{
		int j;
		for (j = i; j < len; ++j)
			data[j] = data[j + 1];
		--len;
		T *now = new T[cap];
		for (j = 0; j < len; ++j)
		{
			now[j] = data[j];
		}
		delete[] data;
		data = now;
		now = NULL;
	}
}

template <class T>
Sqlist<T>& Sqlist<T>::operator=(const Sqlist &rhs)//定义=运算符
{
	data = rhs.data;
	len = rhs.len;
	cap = rhs.cap;
	return *this;
}

template<class T>
Sqlist<T>::Sqlist(const Sqlist &rhs)//拷贝构造函数
{
	data = rhs.data;
	len = rhs.len;
	cap = rhs.cap;
	return *this;
}

猜你喜欢

转载自blog.csdn.net/chczy1/article/details/78127824