#include <iostream>
#ifdef _DEBUG #ifndef DEBUG_NEW #define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) #define new DEBUG_NEW #endif #endif
using namespace std;
namespace _nmsp1 {
#define InitSize 10 #define IncSize 5
template <typename T> class SeqList { public: SeqList(int length=InitSize); ~SeqList();
public: bool ListInsert(int i, const T& e); bool ListDelete(int i); bool GetElem(int i, T& e); int LocateElem(const T &e);
void DispList(); int ListLength(); void ReverseList();
void IncreaseSize();
private: T* m_data; int m_length; int m_maxsize; };
template <typename T> SeqList<T>::SeqList(int length) { m_data = new T[length]; m_length = 0; m_maxsize = length; }
template <typename T> SeqList<T>::~SeqList() { delete[] m_data; m_length = 0; }
template <typename T> bool SeqList<T>::ListInsert(int i, const T& e) { if (m_length >= m_maxsize) { IncreaseSize(); }
if(i < 1 || i > (m_length+1)) { cout << "元素" << e <<"插入的位置" << i << "不合法,合法的位置是1到" << m_length+1 << "之间!" << endl; return false; }
for (int j = m_length; j >= i; --j) { m_data[j] = m_data[j-1]; } m_data[i-1] = e; cout << "成功在位置为" << i << "处插入元素" << m_data[i - 1] << "!" << endl; m_length++; return true; }
template < typename T> bool SeqList<T>::ListDelete(int i) { if (m_length < 1) { cout << "当前顺序表为空,不能删除任何数据!" << endl; return false; } if (i < 1 || i > m_length) { cout << "删除的位置" << i << "不合法,合法的位置是1到" << m_length << "之 间!" << endl; return false; } cout << "成功删除位置为" << i << "的元素,该元素的值为" << m_data[i - 1] << "!" << endl; for (int j = i ; j < m_length; ++j) { m_data[j-1] = m_data[j]; } m_length--; return true; }
template<class T> bool SeqList<T>::GetElem(int i, T& e) { if (m_length < 1) { cout << "当前顺序表为空,不能获取任何数据!" << endl; return false; }
if (i < 1 || i > m_length) { cout << "获取元素的位置" << i << "不合法,合法的位置是1到" << m_length << "之间!" << endl; return false; } e = m_data[i-1]; cout << "成功获取位置为" << i << "的元素,该元素的值为" << m_data[i - 1] << "!" << endl; return true; }
template<class T> int SeqList<T>::LocateElem(const T& e) { for (int i = 0; i < m_length; ++i) { if (m_data[i] == e) { cout << "值为" << e << "的元素在顺序表中第一次出现的位置为" << i+1 << "!" << endl; return i + 1; } } cout << "值为" << e << "的元素在顺序表中没有找到!" << endl; return -1; }
template<class T> void SeqList<T>::DispList() { for (int i = 0; i < m_length; ++i) { cout << m_data[i] << " "; } cout << endl; }
template<class T> int SeqList<T>::ListLength() { return m_length; } template<class T> void SeqList<T>::ReverseList() { if (m_length <= 1) { return; } T temp; for (int i = 0; i < m_length / 2; ++i) { temp = m_data[i]; m_data[i] = m_data[m_length - i - 1]; m_data[m_length - i - 1] = temp; } }
template<class T> void SeqList<T>::IncreaseSize() { T* p = m_data; m_data = new T[m_maxsize + IncSize]; for (int i = 0; i < m_length; i++) { m_data[i] = p[i]; } m_maxsize = m_maxsize + IncSize; delete[] p; } }
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_nmsp1::SeqList<int> seqobj(10); seqobj.ListInsert(1, 15); seqobj.ListInsert(2, 10); seqobj.ListInsert(30, 8); seqobj.ListDelete(1);
int eval = 0; seqobj.GetElem(1, eval);
int findvalue = 10; seqobj.LocateElem(findvalue); seqobj.ListInsert(2, 100); seqobj.DispList(); cout << seqobj.ListLength() << endl; seqobj.ReverseList(); seqobj.DispList();
cout << "-----------" << endl; for (int i = 3; i < 30; ++i) { seqobj.ListInsert(i, i*2); } seqobj.DispList();
return 0; }
|