线性表顺序存储结构的C++模板类程序源代码

线性表顺序存储结构说白了就是数组,类定义了两个成员变量:一个足够大的数组和元素个数变量。下面是自己写的一个线性表类,可以直接使用或增加成员函数使用。

#ifndef LINEARLIST_H
#define LINEARLIST_H
#include 
const int MAXSIZE=100;
template
class LinearList
{
	private:
		ElementType Data[MAXSIZE];
		int last;
	public:
		LinearList();
		~LinearList(){
		}
		int Find(ElementType);
		void Insert(int ,ElementType);
		void Delete(int);		
		bool IsEmpty();
		bool IsFull();
		void Print();
		ElementType operator[](int);
};


template
LinearList::LinearList():last(-1)
{
}
template
int LinearList::Find(ElementType x)
{
	for(int i=0;i<=last;i++)
	{
		if(Data[i]==x)
		{
			std::cout<<"We find the element "<
void LinearList::Insert(int i ,ElementType x)
{
	if(IsFull())
	{
		std::cout<<"The List is full now!";
		return;
	}	
	for(int j=last;j>=i;j--)
	{
		Data[j+1]=Data[j];
	}
	Data[i]=x;
	last++;
	return;
}
template
void LinearList::Delete(int i)
{
	if(IsEmpty())
	{
		std::cout<<"The List is empty now!"<last+1)
	{
		std::cout<<"The element you delete is out of the list!"<
bool LinearList::IsEmpty()
{
	if(last==-1)
		return true;
	else
		return false;
}
template
bool LinearList::IsFull()
{
	if(last==MAXSIZE)
		return true;
	else 
		return false;
}
template
void LinearList::Print()
{
	std::cout<<"The list is printed in the next:\n";
	for(int i=0;i<=last;i++)
	{
		std::cout<
ElementType LinearList::operator[](int i)
{
	return Data[i];
}
#endif
 
  

你可能感兴趣的:(线性表顺序存储结构的C++模板类程序源代码)