• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C++顺序表的实例代码

c++ 搞代码 4年前 (2022-01-06) 24次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了C++实现顺序表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现顺序表的具体代码,供大家参考,具体内容如下

 #include  using namespace std; typedef int DataType; class SeqList { public: S<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码!网</b>eqList() :_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 << "顺序表为空" < 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 << "顺序表为空" < 0) { int i = 0; for (; i <(int)_size; i++) { cout << _a[i] << " "; } cout << endl; } else { cout << "顺序表为空" < 0) { if (pos  _size) { cout << "pos位置非法" < 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  _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; }; 

以下为测试函数

 #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; } 

以上就是C++顺序表的实例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++顺序表的实例代码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址