C++代码实现顺序表
1 #include <iostream>
2 #include <cassert>
3 #include <cstdlib>
4 #include <ctime>
5 using namespace std;
6 typedef int T;
7 const int defaultSize = 100;
8
9 class SeqList
10 {
11 private:
12 T* data;//指向动态内存分配数组的指针
13 int maxSize;//动态内存分配数组的大小
14 //data数组里的个数,从1开始
15 int last;//标识顺序表中最后一个元素的位置
16 //位置,从零开始
17 void Resize(int newSize);
18 public:
19 SeqList(int sz=defaultSize);//构造函数
20 SeqList(const SeqList& L);//拷贝构造函数
21 SeqList& operator=(const SeqList& L);//赋值运算符函数
22 ~SeqList();//析构函数
23
24 int Size() const;//返回顺序表的容量
25 int Length() const;//返回顺序表中元素的个数
26 int Search(T & x) const;//在顺序表中搜索x
27 int Locate(int i) const;//检验顺序表中是否存在第i个元素
28 bool GetData(int i, T& x) const;//获取顺序表中第i个位置的元素
29 void SetData(int i, T& x);//将顺序表中第i个位置的元素赋值为x
30 bool IsEmpty() const;//顺序表是否为空
31 bool IsFull() const;//顺序表是否为满
32 bool Insert(int i, const T& x) ;//在顺序表的第i个元素的位置插入x
33 bool Remove(int i, T&x) ;//删除顺序表第i个位置的元素
34
35 void Sort();//对顺序表中元素进行排序
36 friend istream& operator>>(istream& in, SeqList & L);//输入运算符重载
37 friend ostream& operator<<(ostream& out, const SeqList& L);//输出运算符重载
38 void Reverse();//逆置顺序表中的元素
39 };
40
41 SeqList::SeqList(int sz)
42 {
43 if(sz > 0)
44 {
45 maxSize = sz;
46 last = -1;
47 data = new T[maxSize];
48 if(data == NULL)
49 {
50 cout << "data is wrong!" << endl;
51 exit(1);
52 }
53 }
54 else
55 cout << "number is wrong!" << endl;
56 }
57
58 SeqList::SeqList(const SeqList& L)
59 {
60 maxSize = L.maxSize;
61 last = L.last;
62 data = new T[maxSize];
63 if(data == NULL)
64 {
65 cout << "memery is wrong!" << endl;
66 exit(1);
67 }
68 for(int i=0; i<=last; i++)
69 {
70 data[i]=L.data[i];
71 }
72 }
73
74 SeqList& SeqList::operator=(const SeqList&L)
75 {
76 if(this==&L)
77 return *this;
78 delete []data;
79
80 maxSize = L.maxSize;
81 last = L.last;
82 data = new T[maxSize];
83 if(data == NULL)
84 {
85 cout << "memery is wrong!" << endl;
86 exit(1);
87 }
88 for(int i=0; i<last; ++i)
89 data[i] = L.data[i];
90 return *this;
91 }
92
93 SeqList::~SeqList()
94 {
95 delete[]data;
96 }
97
98 int SeqList::Size() const
99 {
100 return maxSize;
101 }
102
103 int SeqList::Length() const
104 {
105 return last+1;
106 }
107
108 void SeqList::Resize(int newsize)
109 {
110 if(newsize <= 0) //检查是否合法
111 return;
112 if(newsize != maxSize)
113 {
114 T* newAry = new T[newsize];
115 if(newAry == NULL)
116 exit(1);
117 int n = last+1;
118 T* srcptr = data;
119 T* destptr = newAry;
120 while(n--)
121 *destptr++ = *srcptr++; //指针赋值
122 delete[]data;
123 data = newAry;
124 maxSize = newsize;
125 }
126 }
127
128 int SeqList::Search(T& x)const
129 {
130 for(int i=0; i<=last; i++)
131 if(data[i]==x)
132 return i+1;
133 return 0;
134 }
135
136 int SeqList::Locate(int i)const
137 {
138 if(i>=1 && i<=last+1)
139 return i;
140 else
141 return 0;
142 }
143
144 bool SeqList::GetData(int i, T& x) const
145 {
146 if(Locate(i))
147 {
148 x=data[i-1];
149 return true;
150 }
151 else
152 return false;
153 }
154
155 void SeqList::SetData(int i, T& x)
156 {
157 if(Locate(i))
158 data[i-1]=x;
159 }
160
161 bool SeqList::IsEmpty()const
162 {
163 if(last == -1)
164 return true;
165 else
166 return false;
167 }
168
169 bool SeqList::IsFull()const
170 {
171 if(last == maxSize-1)
172 return true;
173 else
174 return false;
175 }
176
177 bool SeqList::Insert(int i, const T& x)
178 {
179 if(last == maxSize-1) //如果满了,扩大空间
180 {
181 Resize(defaultSize);
182 }
183 if(i<0||i>last+1) //不在长度之内,给出提示,不插入
184 return false;
185 for(int j=last; j>=i-1; j--)
186 data[j+1]=data[j];
187 data[i-1] = x;
188 last++;
189 return true;
190 }
191
192 bool SeqList::Remove(int i,T& x)
193 {
194 if(last == -1) //空表,给出提示
195 {
196 cout << "empty" << endl;
197 return false;
198 }
199 if(!Locate(i))
200 return false;
201 x=data[i-1];
202 for(int j=i-1; j<last; j++)
203 data[j]=data[j+1];
204 last--;
205 return true;
206 }
207
208 void SeqList::Sort()
209 {
210 for(int i=0; i<=last; i++)
211 for(int j=i; j<last; j++)
212 {
213 // cout << data[i] << ' ' << data[j] << endl;
214 if(data[i]>data[j])
215 swap(data[i],data[j]);
216 }
217
218 }
219
220 void SeqList::Reverse()
221 {
222 for(int i=0,j=last-1; i<j; i++,j--)
223 {
224 //cout << data[i] << ' ' << data[j] << endl;
225 swap(data[i],data[j]);
226 }
227 }
228
229 istream& operator>>(istream& in, SeqList & L)
230 {
231 while(1)
232 {
233 cout << "The number of the list: ";
234 cin>>L.last;
235 if(L.last<=L.maxSize-1)
236 break;
237 else
238 {
239 cout << "please try again." << endl;;
240 }
241 }
242 for(int i=0; i< L.last; i++)
243 {
244 cin >> L.data[i];
245
246 }
247 return in;
248 }
249
250 ostream& operator<<(ostream& out, const SeqList& L)
251 {
252 cout << "The number of the list: ";
253 for(int i=0; i< L.last; i++)
254 cout << L.data[i] <<' ';
255 cout << endl;
256 return out;
257 }
258
259 int main()
260 {
261 SeqList sList;
262
263 cin >> sList;
264 cout<<sList;
265
266 cout << "insert: ";
267 int i, val;
268 cin>>i>>val;
269 sList.Insert(i,val);
270 cout<<sList;
271
272 cout << "remove: ";
273 cin>>i;
274 sList.Remove(i,val);
275 cout<<sList;
276
277 cout << "search: ";
278 cin>>val;
279 i=sList.Search(val);
280 if(i==0)
281 cout<<"Not found"<<endl;
282 else
283 cout<<i<<endl;
284
285 cout << "reverse: " << endl;
286 sList.Reverse();
287 cout<<sList;
288
289 cout << "sort: " << endl;
290 sList.Sort();
291 cout<<sList;
292
293 SeqList L2;
294 L2=sList;
295 cout << "L2 is :" << L2;
296 }
测试结果