顺序表类定义:
1 template<class T> 2 class SeqList : 3 { 4 public: 5 SeqList(int mSize); 6 ~SeqList() 7 { 8 delete[] elements; 9 } 10 bool SM_Delete(T x); 11 private: 12 int maxLength; 13 T *elements; 14 }; 15 template <class T> 16 SeqList<T>::SeqList(int mSize) 17 { 18 maxLength = mSize; 19 elements = new T[maxLength]; 20 n = 0; 21 }
删除所有特定元素x成员函数:
1 /*这里提供两种方式,其中一种为了方便测试,作为注释附带*/ 2 template <class T> 3 bool SeqList<T>::SM_Delete(T x) 4 { 5 6 for (int i = 0; i < n; i++) 7 { 8 if (elements[i] == x) 9 /* 10 for (int j = i+1; j < n; j++) 11 { 12 elements[j-1 ] = elements[j]; 13 } 14 n--; 15 */ 16 Delete(i); 17 } 18 return true; 19 /* 20 for (int i = 0; i < n;i++) 21 Delete(Search(x)); 22 return true; 23 */ 24 }