顺序表(C++语言实现)
2015-09-14 16:07
501 查看
<span style="font-size:24px;">顺序表(C++语言实现) </span>
<span style="font-size:24px;">含有头插尾插、头删尾删、插入、修改、删除、查找等功能</span>
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;typedef int DataType;typedef struct FindRet{bool isFind;size_t index;}FindRet;class SeqList{public:SeqList()//构造函数:_array(NULL), _size(0), _capacity(0){}SeqList(const SeqList& sList)//拷贝构造函数{_array = new DataType[sList._capacity];memcpy(_array, sList._array, sizeof(DataType)*sList._size);}SeqList& operator=(const SeqList& sList)//赋值运算符的重载{}~SeqList(){if (_array){delete[] _array;}_size = 0;_capacity = 0;}public:void PushBack(const DataType& x){_CheckCapacity();_array[_size++] = x;}void PopBack(){if (_size)--_size;}void PushFront(const DataType& x){_CheckCapacity();size_t i = _size;for (; i > 0; i--){_array[i] = _array[i-1];}_array[0] = x;_size++;}void PopFront(){if (_size){int i = 0;for (; i < _size; i++){_array[i] = _array[i + 1];}--_size;}}void DisPlay(){size_t i = 0;for ( ; i < _size; ++i){cout << _array[i] << "->" ;}cout << endl;}void Insert(size_t index, const DataType& x ){_CheckCapacity();size_t i = _size;for (; i > index; i--){_array[i] = _array[i - 1];}_array[index] = x;_size++;}void Modified(size_t index, const DataType& x){_array[index] = x;}void Remove(size_t index){for (; index < _size; index++){_array[index] = _array[index + 1];}--_size;}FindRet Find(SeqList* pSeq, const DataType& x, size_t index){FindRet ret;for (; index < _size; index++){if (x == _array[index]){ret.isFind = true;ret.index = index;cout << "is Find." << endl;return ret;}ret.isFind = false;cout << "isn't Find." << endl;return ret;}}private:DataType* _array;size_t _size;size_t _capacity;private:void _CheckCapacity(){if (_size == _capacity){_capacity = 2 * _capacity + 3;DataType* tmp = new DataType[_capacity];memcpy(tmp,_array,sizeof(DataType)*_size);delete[] _array;_array = tmp;}}};void Test1(){SeqList s1;s1.PushBack(1);s1.PushBack(2);s1.PushBack(3);s1.PushBack(4);s1.PushBack(5);s1.PushBack(6);s1.DisPlay();s1.PopBack();s1.PopBack();s1.PopBack();s1.DisPlay();}void Test2(){SeqList s2;s2.PushFront(1);s2.PushFront(2);s2.PushFront(3);s2.PushFront(4);s2.PushFront(5);s2.PushFront(6);s2.DisPlay();s2.PopFront();s2.PopFront();s2.PopFront();s2.DisPlay();}void Test3(){SeqList s3;s3.PushBack(1);s3.PushBack(2);s3.PushBack(3);s3.PushBack(4);s3.PushBack(5);s3.PushBack(6);s3.DisPlay();s3.Modified(2, 8);s3.DisPlay();s3.Find(&s3, 1, 0);s3.Remove(1);s3.DisPlay();s3.Insert(5, 9);s3.DisPlay();}int main(){Test1();Test2();Test3();system("pause");return 0;}
相关文章推荐
- 设计模式C++实现(10)——桥接模式
- 设计模式C++实现(9)——享元模式
- ffmpeg的安装&&在c++工程中引用ffmpeg类库
- 《Java编程思想》作者:C++不垃圾,只是Java很傲慢
- c++primer plus 第九章-名称空间
- C++中shared_ptr的使用
- C语言获取文件大小
- VC++、MFC中最好的开源项目
- C++中memset()函数的作用
- 剖析C++编程当中指针作为函数参数的用法
- hanoi 并计算移动次数 c语言
- C++编译机制(vc++)
- C语言字符串反转实现【采用头指针和尾指针方法完成】
- C++调用webservice
- QT学习之路-C++类的前置声明
- C++Primer第五版 4.1.2节练习
- C++面向对象高级开发课程(第三周)
- C++ STL — 第6章 STL容器(三)list
- KMP算法的C语言实现
- MFC主要知识点