码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构顺序表删除所有特定元素x

时间:2015-09-13 15:48:55      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

顺序表类定义:

 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 }

 

数据结构顺序表删除所有特定元素x

标签:

原文地址:http://www.cnblogs.com/zlgxzswjy/p/4804961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!