数据结构——顺序表

数据结构——顺序表

const int defaultSize = 10;
template <typename DataType>
class SeqList{
private:
	DataType* elements;
	int maxSize;
	int length;
public:
	SeqList(int size = defaultSize){
		maxSize = size;
		elements = new DataType[maxSize];
	}
	~SeqList()
	{
		delete[] elements;
	}
	bool insertElement(DataType data);
	bool deleteElement(int location);
	DataType getElement(int location);
	bool changeElement(int location,DataType newData); 
};
template <typename DataType>
bool SeqList<DataType>::insertElement(DataType data)
{
	if(length>=maxSize)
	{
		return false;
	}else{
	elements[length]=data;
	length++;
	return true;
}
}
template <typename DataType>
bool SeqList<DataType>::deleteElement(int location)
{
	if(location<=0||location >=length||length<=0)
	{
		return false;
	}
	else{
		for(int i = location;i<length;i++)
		{
			elements[i-1]=elements[i];
		}
		length--;
		return true;
	}
}
template <typename DataType>
DataType SeqList<DataType>::getElement(int location)
{
		if(location<=0||location >=length)
	{
		return false;
	}else
	{
		return elements[location];
	}
}
template <typename DataType>
bool SeqList<DataType>::changeElement(int location,DataType newData);
{
		if(location<=0||location >=length)
	{
		return false;
	}else{
		elements[location-1]=newData;
		return true;
	}
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页