本文实例为大家分享了C++实现顺序表的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <iostream> using namespace std; typedef int DataType; class SeqList { public : SeqList() :_a(NULL) , _size(0) , _capacity(0) {} SeqList( const SeqList& s) :_a( new DataType[s._size]) , _size(s._size) , _capacity(s._capacity) { memcpy (_a, s._a, sizeof (DataType)*s._size); } SeqList& operator=( const SeqList& s) { if ( this != &s) { DataType* tmp = new DataType[s._size]; delete [] _a; _a = tmp; memcpy (_a, s._a, sizeof (DataType)*s._size); _size = s._size; _capacity = s._capacity; } return * this ; } //SeqList& operator=(SeqList s) //若传引用会改变引用对象的值 //{ // swap(_a, s._a); // swap(_size, s._size); // swap(_capacity, s._capacity); // return *this; //} ~SeqList() { if (_a) { delete [] _a; } } void PushBack(DataType d) { CheckCapacity(); _a[_size] = d; _size++; } void PopBack() { if (_size > 0) { _size--; } else { cout << "顺序表为空" << endl; } } void PushFront(DataType d) { CheckCapacity(); int i = ( int )_size; for (; i > 0; i--) { _a[i] = _a[i - 1]; } _a[0] = d; ++_size; } void PopFront() { if (_size > 0) { int i = 0; for (; i < ( int )_size; i++) { _a[i] = _a[i + 1]; } _size--; } else { cout << "顺序表为空" << endl; } } void Print() { if (_size > 0) { int i = 0; for (; i < ( int )_size; i++) { cout << _a[i] << " " ; } cout << endl; } else { cout << "顺序表为空" << endl; } } void Insert( size_t pos, DataType d) //在pos之前插入一个数据 { CheckCapacity(); if (_size > 0) { if (pos <= 0 || pos > _size) { cout << "pos位置非法" << endl; } else { int i = 0; for (i = ( int )_size + 1; i > pos - 1; i--) { _a[i] = _a[i - 1]; } _a[pos - 1] = d; _size++; } } else { PushFront(d); } } void Erase( size_t pos) //删除pos位置的数据 { if (_size > 0) { if (pos <= 0 || pos > _size) { cout << "pos位置非法" << endl; } else { int i = pos - 1; for (; i < ( int )_size; i++) { _a[i] = _a[i + 1]; } _size--; } } else { cout << "顺序表为空,无法进行删除" << endl; } } int Find(DataType d) { int i = 0; for (; i < ( int )_size; i++) { if (_a[i] == d) { return i + 1; } } return 0; } private : void CheckCapacity() { if (_size == _capacity) { _capacity = _capacity * 2 + 3; _a = (DataType*) realloc (_a, sizeof (DataType)*_capacity); } } private : DataType* _a; size_t _size; size_t _capacity; }; |
以下为测试函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include "SeqList.h"; void Test1() { SeqList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); SeqList s2(s1); s2.Print(); s2.PopBack(); s2.PopBack(); s2.PopBack(); s2.PopBack(); s2.PopBack(); s2.Print(); s2.PushFront(4); s2.PushFront(3); s2.PushFront(2); s2.PushFront(1); s2.Print(); s2.PopFront(); s2.Print(); s2.PopFront(); s2.PopFront(); s2.PopFront(); s2.PopFront(); s2.PopFront(); SeqList s3; s3 = s1; s3.Print(); } void Test2() { SeqList s1; s1.PushBack(1); s1.PushBack(2); s1.PushBack(3); s1.PushBack(4); s1.Print(); //s1.Insert(1, 0); //s1.Print(); /*s1.Erase(1); s1.Erase(1); s1.Erase(1); s1.Erase(1); s1.Print();*/ int i = s1.Find(5); cout << i << endl; } int main() { //Test1(); Test2(); system ( "pause" ); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/tttjp/article/details/69663442