一个完整顺序表的实现

【linearlist.h】:

 1 #include<iostream>
 2 using namespace std;
 3 template <class T>
 4 class Linearlist
 5 {
 6 public:
 7     virtual bool IsEmpty() const = 0;
 8     virtual int Length() const = 0;
 9     virtual bool Find(int i, T& x)const = 0;
10     virtual int Search(T x) const = 0;
11     virtual bool Insert(int i, T x) = 0;
12     virtual bool Delete(int i) = 0;
13     virtual bool Update(int i, T x) = 0;
14     virtual void Output(ostream & out) const = 0;
15 protected:
16     int n;
17 };

【seqlist.h】:

  1 #include"linearlist.h"
  2 template<class T>
  3 class SeqList :public Linearlist<T>
  4 {
  5 public:
  6     SeqList(int mSize);
  7     ~SeqList()
  8     {
  9         delete[] elements;
 10     }
 11     bool IsEmpty() const;
 12     int Length() const;
 13     bool Find(int i, T& x) const;
 14     int Search(T x) const;
 15     bool Insert(int i, T x);
 16     bool Delete(int i);
 17     bool Update(int i, T x);
 18     void Output(ostream& out) const;
 19 private:
 20     int maxLength;
 21     T *elements;
 22 };
 23 
 24 template <class T>
 25 SeqList<T>::SeqList(int mSize)
 26 {
 27     maxLength = mSize;
 28     elements = new T[maxLength];
 29     n = 0;
 30 }
 31 template <class T>
 32 bool SeqList<T>::IsEmpty() const
 33 {
 34     return n == 0;
 35 }
 36 template <class T>
 37 int SeqList<T>::Length() const
 38 {
 39     return n;
 40 }
 41 template <class T>
 42 bool SeqList<T>::Find(int i, T& x) const
 43 {
 44     if (i<-1 || i>n - 1)
 45     {
 46         cout << "out of bounds" << endl;
 47         return false;
 48     }
 49     x = elements[i];
 50     return true;
 51 }
 52 template <class T>
 53 int SeqList<T>::Search(T x) const
 54 {
 55     for (int j = 0; j < n; j++)
 56     if (elements[j] == x)
 57         return j;
 58     return -1;
 59 }
 60 template <class T>
 61 bool SeqList<T>::Insert(int i, T x)
 62 {
 63     if (i<-1 || i>n - 1)
 64     {
 65         cout << "out of bound" << endl;
 66         return false;
 67     }
 68     if (n == maxLength)
 69     {
 70         cout << "overflow" << endl;
 71         return false;
 72     }
 73     for (int j = n - 1; j > i; j--)
 74     {
 75         elements[j + 1] = elements[j];
 76     }
 77     elements[i + 1] = x;
 78     n++;
 79     return true;
 80 }
 81 template <class T>
 82 bool SeqList<T>::Delete(int i)
 83 {
 84     if (!n)
 85     {
 86         cout << "underflow" << endl;
 87         return false;
 88     }
 89     if (i<-1 || i>n - 1)
 90     {
 91         cout << "out of bound" << endl;
 92         return false;
 93     }
 94     /*
 95     for (int j = i; j <= n - 1; j++)
 96     elements[j] = elements[j + 1];
 97     */
 98     for (int j = i + 1; j < n; j++)
 99         elements[j - 1] = elements[j];
100     n--;
101     return true;
102 }
103 template<class T>
104 bool SeqList<T>::Update(int i, T x)
105 {
106     if (i<-1 || i>n - 1)
107     {
108         cout << "out of bound" << endl;
109         return false;
110     }
111     elements[i] = x;
112     return true;
113 }
114 template<class T>
115 void SeqList<T>::Output(ostream& out)const
116 {
117     for (int j = 0; j < n; j++)
118         out << elements[j] << " ";
119     cout << endl;
120 }

【seqlist_new.h】:

  1 #include"linearlist.h"
  2 template<class T>
  3 class SeqList :public Linearlist<T>
  4 {
  5 public:
  6     SeqList(int mSize);
  7     ~SeqList()
  8     {
  9         delete[] elements;
 10     }
 11     bool IsEmpty() const;
 12     int Length() const;
 13     bool Find(int i, T& x) const;
 14     int Search(T x) const;
 15     bool Insert(int i, T x);
 16     bool Delete(int i);
 17     bool Update(int i, T x);
 18     void Output(ostream& out) const;
 19     bool SM_Delete(T x);
 20 private:
 21     int maxLength;
 22     T *elements;
 23 };
 24 
 25 template <class T>
 26 SeqList<T>::SeqList(int mSize)
 27 {
 28     maxLength = mSize;
 29     elements = new T[maxLength];
 30     n = 0;
 31 }
 32 template <class T>
 33 bool SeqList<T>::IsEmpty() const
 34 {
 35     return n == 0;
 36 }
 37 template <class T>
 38 int SeqList<T>::Length() const
 39 {
 40     return n;
 41 }
 42 template <class T>
 43 bool SeqList<T>::Find(int i, T& x) const
 44 {
 45     if (i<-1 || i>n - 1)
 46     {
 47         cout << "out of bounds" << endl;
 48         return false;
 49     }
 50     x = elements[i];
 51     return true;
 52 }
 53 template <class T>
 54 int SeqList<T>::Search(T x) const
 55 {
 56     for (int j = 0; j < n; j++)
 57     if (elements[j] == x)
 58         return j;
 59     return -1;
 60 }
 61 template <class T>
 62 bool SeqList<T>::Insert(int i, T x)
 63 {
 64     if (i<-1 || i>n - 1)
 65     {
 66         cout << "out of bound" << endl;
 67         return false;
 68     }
 69     if (n == maxLength)
 70     {
 71         T *elements_temp;
 72         elements_temp = new T[2 * maxLength];
 73         for (int k = 0; k<n; k++)
 74         {
 75             elements_temp[k] = elements[k];
 76         }
 77         delete[]elements;
 78         elements = elements_temp;
 79     }
 80     for (int j = n - 1; j > i; j--)
 81     {
 82         elements[j+1 ] = elements[j];
 83     }
 84     elements[i+1] = x;
 85     n++;
 86     return true;
 87 }
 88 template <class T>
 89 bool SeqList<T>::Delete(int i)
 90 {
 91     if (!n)
 92     {
 93         cout << "underflow" << endl;
 94         return false;
 95     }
 96     if (i<-1 || i>n - 1)
 97     {
 98         cout << "out of bound" << endl;
 99         return false;
100     }
101     /*
102     for (int j = i; j <= n - 1; j++)
103     elements[j] = elements[j + 1];
104     */
105     for (int j = i + 1; j < n; j++)
106         elements[j - 1] = elements[j];
107     n--;
108     return true;
109 }
110 template<class T>
111 bool SeqList<T>::Update(int i, T x)
112 {
113     if (i<-1 || i>n - 1)
114     {
115         cout << "out of bound" << endl;
116         return false;
117     }
118     elements[i] = x;
119     return true;
120 }
121 template<class T>
122 void SeqList<T>::Output(ostream& out)const
123 {
124     for (int j = 0; j < n; j++)
125         out << elements[j] << " ";
126     cout << endl;
127 }
128 /*
129 bool SeqList<T>::SM_Delete(T x)
130 {
131     T *temp = new T[n];
132     int N=0;
133     for (int i = 0; i < n ; i++)
134     {
135         if (elements[i] == x)
136         {
137             *temp = i;
138             temp++;
139             N++;
140         }
141     }
142     for (int j = 0; j < N; j++)
143     {
144         Delete(temp[j]);
145         for (int k = j + 1; k < N; k++)
146             temp[k]--;
147     }
148     return true;
149 }
150 */
151 template <class T>
152 bool SeqList<T>::SM_Delete(T x)
153 {
154     
155     for (int i = 0; i < n; i++)
156     {
157         if (elements[i] == x)
158             /*
159         for (int j = i+1; j < n; j++)
160         {
161         elements[j-1 ] = elements[j];
162         }
163         n--;
164         */
165         Delete(i);
166     }
167     return true;
168     /*
169     for (int i = 0; i < n;i++)
170     Delete(Search(x));
171     return true;
172     */
173 }

【源.cpp】:

 1 #include"seqlist_new.h"
 2 int main()
 3 {
 4     SeqList<int> A(3);
 5     A.Insert(-1, 1);
 6     A.Insert(0, 2);
 7     A.Insert(1, 3);
 8     A.Insert(2, 2);
 9     A.Insert(3, 4);
10     A.Insert(4, 2);
11     A.Insert(5, 5);
12     A.Output(cout);
13     A.SM_Delete(2);
14     A.Output(cout);
15     system("pause");
16     return 0;
17 }

 

转载于:https://www.cnblogs.com/zlgxzswjy/p/4804965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值