#ifndef SqList_H
#define SqList_H
#include <memory.h>
template<class ElemType>
class SqList
{
public:
SqList(void);
~SqList(void);
//************************************
// Method: AddElement 增加元素
// FullName: SqList<ElemType>::AddElement
// Access: virtual public
// Returns: bool
// Qualifier:
// Parameter: ElemType _elem
//************************************
virtual bool AddElement(ElemType _elem);
//************************************
// Method: InsertElement 插入元素
// FullName: SqList<ElemType>::InsertElement
// Access: virtual public
// Returns: bool
// Qualifier:
// Parameter: ElemType _elem
//************************************
virtual bool InsertElement(int _index,ElemType _elem);
//************************************
// Method: DeleteElement 删除元素
// FullName: SqList<ElemType>::DeleteElement
// Access: virtual public
// Returns: bool
// Qualifier:
// Parameter: int _index
//************************************
virtual bool DeleteElement(ElemType _elem);
//************************************
// Method: DeleteElementByIndex 通过下标删除元素
// FullName: SqList<ElemType>::DeleteElementByIndex
// Access: virtual public
// Returns: bool
// Qualifier:
// Parameter: int _index
//************************************
virtual bool DeleteElementByIndex(int _index);
//************************************
// Method: FindElement 查找元素
// FullName: SqList<ElemType>::FindElement
// Access: virtual public
// Returns: int
// Qualifier:
// Parameter: ElemType _elem
//************************************
virtual int FindElement(ElemType _elem);
virtual void Print();
private:
ElemType* elem;
int length;
};
template<class ElemType>
SqList<ElemType>::SqList(void)
{
this->elem = new ElemType;
memset(this->elem, 0, sizeof(this->elem));
this->length = 0;
}
template<class ElemType>
SqList<ElemType>::~SqList(void)
{
delete[] this->elem;
this->elem = NULL;
}
template<class ElemType>
int SqList<ElemType>::FindElement(ElemType _elem)
{
cout << "查找元素:" << _elem << endl;
int index = 0;
while (index < length)
{
if (this->elem[index++] == _elem)
return index;
}
return -1;
}
template<class ElemType>
void SqList<ElemType>::Print()
{
int index = 0;
while (index < length)
{
cout << elem[index++] << " ";
}
cout << endl;
}
template<class ElemType>
bool SqList<ElemType>::AddElement(ElemType _elem)
{
cout << "添加元素:" << _elem << endl;
ElemType* pTemp = new ElemType[this->length + 1];
memset(pTemp, 0, sizeof(pTemp) * (this->length + 1));
memcpy(pTemp, this->elem, sizeof(this->elem) * (this->length));
delete[] this->elem;
this->elem = new ElemType[this->length + 1];
memcpy(this->elem, pTemp, sizeof(pTemp) * (this->length + 1));
delete[] pTemp;
pTemp = NULL;
this->length++;
this->elem[this->length - 1] = _elem;
return true;
}
template<class ElemType>
bool SqList<ElemType>::InsertElement(int _index, ElemType _elem)
{
cout << "在第 " << _index << " 位 插入元素:" << _elem << endl;
_index--;
ElemType* pTemp = new ElemType[this->length + 1];
memset(pTemp, 0, sizeof(pTemp) * (this->length + 1));
memcpy(pTemp, this->elem, sizeof(this->elem) * _index);
pTemp[_index] = _elem;
memcpy(pTemp + _index + 1, this->elem + _index, sizeof(this->elem) * (this->length - _index));
delete[] this->elem;
this->elem = new ElemType[this->length + 1];
memcpy(this->elem, pTemp, sizeof(pTemp) * (this->length + 1));
this->length++;
delete[] pTemp;
pTemp = NULL;
return true;
}
template<class ElemType>
bool SqList<ElemType>::DeleteElement(ElemType _elem)
{
cout << "删除元素:" << _elem << endl;
if (this->length == 0) return false;
int index = 0;
while ( index < length )
{
if ( this->elem[index] == _elem )
break;
index++;
}
if (index >= length) return false;
ElemType* pTemp = new ElemType[this->length - 1];
memset(pTemp, 0, sizeof(pTemp) * (this->length - 1));
memcpy(pTemp, this->elem, sizeof(this->elem) * (index));
memcpy(pTemp + index, this->elem + index + 1, sizeof(this->elem) * (this->length - index));
delete[] this->elem;
this->elem = new ElemType[this->length - 1];
memcpy(this->elem, pTemp, sizeof(pTemp) * (this->length - 1));
this->length--;
delete[] pTemp;
pTemp = NULL;
return true;
}
template<class ElemType>
bool SqList<ElemType>::DeleteElementByIndex(int _index)
{
cout << "删除第 " << _index << " 位元素" << endl;
if (_index < 1 || _index > length || length == 0) return false;
_index--;
ElemType* pTemp = new ElemType[this->length - 1];
memset(pTemp, 0, sizeof(pTemp) * (this->length - 1));
memcpy(pTemp, this->elem, sizeof(this->elem) * (_index));
memcpy(pTemp + _index, this->elem + _index + 1, sizeof(this->elem) * (this->length - _index));
delete[] this->elem;
this->elem = new ElemType[this->length - 1];
memcpy(this->elem, pTemp, sizeof(pTemp) * (this->length - 1));
this->length--;
delete[] pTemp;
pTemp = NULL;
return true;
}
#endif
C++顺序表操作
最新推荐文章于 2022-12-09 16:14:29 发布