顺序表的实现


  1 #include<iostream>
  2 using namespace std;
  3 #include<cassert>
  4 typedef  int DataType;
  5 
  6 class SeqList
  7 {
  8 public:
  9     SeqList()
 10         : _array(NULL)
 11         , _size(0)
 12         , _capacity(0)
 13     {
 14         cout << "SeqList()" << endl;
 15     }
 16     SeqList(DataType* a, size_t size)
 17         : _array(new DataType[size])
 18         , _size(size)
 19         , _capacity(size)
 20     {
 21         cout << "SeqList()" << endl;
 22         memcpy(_array, a, sizeof(DataType)*size);
 23     }
 24     ~SeqList()
 25     {
 26         if (_array)
 27         {
 28             delete[] _array;
 29             _array = NULL;
 30             _size = 0;
 31             _capacity = 0;
 32         }
 33         cout << "~SeqList()" << endl;
 34     }
 35     /*SeqList(const SeqList &s)
 36     {
 37         _array = new DataType[s._size];
 38         memcpy(_array, s._array, sizeof(DataType)*s._size);
 39         _size = s._size;
 40         _capacity = s._capacity;
 41     }*/
 42     SeqList(const SeqList &s)
 43         :_array(NULL)
 44         , _size(0)
 45         , _capacity(0)
 46     {
 47         SeqList tmp (s._array,s._size);
 48         tmp.Swap(*this);
 49     }
 50     void Swap(SeqList &s)
 51     {
 52         std::swap(_array, s._array);
 53         std::swap(_size, s._size);
 54         std::swap(_capacity, s._capacity);
 55     }
 56     SeqList& operator=(const SeqList &s)
 57     {
 58         if (this != &s)
 59         {
 60             delete[] _array;
 61             _array = new DataType[s._size];
 62             _size = s._size;
 63             _capacity = s._capacity;
 64         }
 65         return *this;
 66     }
 67 public:
 68     void PushBack(DataType x)
 69     {
 70         _CheckCapacity(_size);
 71         _array[_size] = x;
 72         _size++;
 73     }
 74 
 75     void PushFront(DataType x)
 76     {
 77         _CheckCapacity(_size);
 78         for (size_t i = _size; i >0; --i)
 79         {
 80             _array[i] = _array[i - 1];
 81         }
 82         _array[0] = x;
 83         _size++;
 84     }
 85 
 86     void Insert(DataType x, size_t Pos)
 87     {
 88         _CheckCapacity(_size);
 89         if (Pos <= 0 || Pos>_size)
 90         {
 91             cout << "插入位置不合法" << endl;
 92         }
 93         else
 94         {
 95             for (size_t i = _size; i > Pos; --i)
 96             {
 97                 _array[i] = _array[i-1];
 98             }
 99             _array[Pos] = x;
100             ++_size;
101         }
102     }
103 
104     void PopFront()
105     {
106         assert(_array);
107         if (_size <= 0)
108         {
109             cout << "顺序表为空。" << endl;
110         }
111         else
112         {
113             for (size_t i = 1; i <_size; ++i)
114             {
115                 _array[i - 1] = _array[i];
116             }
117             _size--;
118         }
119         
120     }
121 
122     void PopBack()
123     {
124         assert(_array);
125         if (_size <= 0)
126         {
127             cout << "顺序表为空。" << endl;
128         }
129         else
130         {
131             _size--;
132         }
133         
134     }
135 
136     void Erase(size_t Pos)
137     {
138         assert(_array);
139         if (_size <= 0)
140         {
141             cout << "顺序表为空。" << endl;
142         }
143         else if (Pos <= 0 || Pos>_size)
144         {
145             cout << "指定位置不合法" << endl;
146         }
147         else
148         {
149             for (size_t i = Pos; i < _size-1; ++i)
150             {
151                 _array[i] = _array[i + 1];
152             }
153             _size--;
154         }
155     }
156 
157     int Find(const DataType& x)
158     {
159         assert(_array);
160         size_t result=0;
161         for (size_t i = 0; i < _size; i++)
162         {
163             if (_array[i] == x)
164             {
165                 result = i;
166                 break;
167             }
168             else
169             {
170                 result = -1;
171             }
172         }
173         if (result == -1)
174         {
175             cout << "顺序表中没有要查找的数据。" << endl;
176         }
177         return result;
178     
179     }
180 
181     void Print()
182     {
183         for (size_t i = 0; i < _size; i++)
184         {
185             cout << _array[i] << " ";
186         }
187         cout << endl;
188     }
189 
190 private:
191     void  _CheckCapacity(size_t size)
192     {
193         if (size >= _capacity)
194         {
195             _capacity = size > _size * 2 ? size : _capacity * 2;
196             DataType* tmp = new DataType[_capacity];
197             memcpy(tmp, _array, sizeof(DataType)*_size);
198             delete[] _array;
199             _array = tmp;
200         }
201     }
202 private:
203     DataType* _array;
204     size_t _size;
205     size_t _capacity;
206 };
207 
208 void Test1()
209 {
210     int array[5] = { 0, 1, 2, 3, 4 };
211     SeqList s1(array, 5);
212     cout << "原顺序表:" << endl;
213     s1.Print();
214     cout << "PushBack:" << endl;
215     s1.PushBack(5);
216     s1.PushBack(6);
217     s1.PushBack(7);
218     s1.PushBack(8);
219     s1.PushBack(9);
220     s1.PushBack(10);
221 
222 
223     s1.Print();
224 
225     cout << "PushFront(5):" << endl;
226     s1.PushFront(5);
227     s1.Print();
228 
229     cout << "Insert(5,2):" << endl;
230     s1.Insert(5,2);
231     s1.Print();
232 
233 }
234 
235 void Test2()
236 {
237     int array[5] = { 0, 1, 2, 3, 4 };
238     SeqList s2(array, 5);
239     cout << "原顺序表:" << endl;
240     s2.Print();
241     cout << "PopFront():" << endl;
242     s2.PopFront();
243     s2.PopFront();
244     //s2.PopFront();
245     s2.Print();
246 
247     cout << "PopBack():" << endl;
248     s2.PopBack();
249     s2.PopBack();
250     //s2.PopBack();
251     //s2.PopBack();
252     s2.Print();
253 
254     cout << "Erase(2):" << endl;
255     s2.Erase(2);
256     s2.Print();
257 }
258 
259 void Test3()
260 {
261     int array[5] = { 8, 3, 4, 6, 5 };
262     SeqList s3(array, 5);
263     cout << "原顺序表:" << endl;
264     s3.Print();
265 
266     cout << "查找3所对应的位置:" << s3.Find(3) << endl;
267     cout << "查找9所对应的位置:" << s3.Find(9) << endl;
268 
269 }
270 int main()
271 {
272     Test1();
273     Test2();
274     Test3();
275     return 0;
276 }


 
原文地址:https://www.cnblogs.com/hanxiaoyu/p/5401470.html