主要功能:
1、初始化顺序表
2、顺序表的头插
3、顺序表的尾插
4、顺序表的头删
5、顺序表的尾删
6、顺序表的任意点插入
7、顺序表的查找删除
8、顺序表的排序
9、清空顺序表
10、打印顺序表
代码实现:
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
template<class T>
class Vector{
public:
Vector() //带缺省值的构造函数
:_start(NULL)
, _finish(NULL)
, _endofStorage(NULL)
{}
Vector(T* array, int size) //构造函数
:_start(new T[size])
{
//方法一
//memcpy(_start,array,size*sizeof(T));
//方法二
for (int i = 0; i < size; ++i)
{
_start[i] = array[i];
}
_finish = _start + size;
_endofStorage = _start + size;
}
Vector(const Vector<T>& s) //拷贝构造函数
{
int size = s._finish-s._start;
_start = new T[size];
for (int i = 0; i < size; ++i)
{
_start[i] = s._start[i];
}
_finish = _start + size;
_endofStorage = _start + size;
}
Vector<T>& operator=(const Vector<T>& s) //赋值运算符重载
{
if (this != &s)
{
size_t size = Size();
T* Temp = new T[size];
for (size_t i = 0; i < size; ++i)
{
_start[i] = s._start[i];
}
delete[] _start;
_start = Temp;
_finish = s._finish;
_endofStorage = s._endofStorage;
}
return *this;
}
~Vector() //析构函数
{
if (_start)
{
delete[] _start;
_start = NULL;
_finish = NULL;
_endofStorage = NULL;
}
}
void CheckCapacity() //扩容
{
if (_finish == _endofStorage)
{
int oldSize = Size();
int newSize = oldSize * 2 + 3;
T* temp = new T[newSize];
for (int i = 0; i < oldSize; ++i)
{
temp[i] = _start[i];
}
delete[] _start;
_start = temp;
_finish = _start + oldSize;
_endofStorage = _start+newSize;
cout << "扩容成功" << endl;
}
}
void PushBack(const T data) //尾插
{
CheckCapacity();
*_finish = data;
_finish++;
}
void PopBack() //尾删
{
if (_start != _finish)
_finish--;
}
void Insert(T pos, const T data) //任意位置的插入
{
assert(pos);
CheckCapacity();
T* cur = _finish - 1;
T* tmp = _finish;
while (*cur != pos)
{
*tmp = *cur;
tmp--;
cur--;
}
*tmp = data;
_finish++;
}
void Delete(T pos)
{
T* cur = _start;
if (_start == _finish)
return;
else
{
while (*cur != pos)
{
cur++;
}
while (cur != _finish-1)
{
*cur = *(cur + 1);
cur++;
}
_finish--;
}
}
void BubbleSort()
{
int size = Size();
int flag = 0;
for (int i = 0; i < size - 1; i++)
{
flag = 0;
for (int j = 0; j < size - 1 - i; j++)
{
if (_start[j]>_start[j + 1])
{
T temp = _start[j];
_start[j] = _start[j + 1];
_start[j + 1] = temp;
flag = 1;
}
}
if (flag == 0)
break;
}
}
void Print()
{
int size = Size();
for (int i = 0; i < size; ++i)
cout << _start[i] << " ";
cout << "元素个数:" << Size() << "容量大小:" << _endofStorage - _start << endl;
}
T& operator[](int Index)
{
assert(Index);
return _start[Index];
}
const T& operator[](int Index)const
{
assert(Index);
return _start[Index];
}
int Size()
{
return _finish - _start;
}
void Destory()
{
_finish = _start;
cout <<"销毁成功"<< endl;
}
bool Empty()
{
return _start == _finish;
}
private:
T* _start; //容器的的开始
T* _finish; //容器元素的个数
T* _endofStorage; //容器的容量
};
void Test()
{
int array[10] = { 1, 2, 7, 4, 5, 6, 7, 8, 9, 10 };
Vector<int> s1(array, sizeof(array) / sizeof(array[0]));
Vector<int> s2(s1);
Vector<int> s3 = s2;
s1.PushBack(6);
s1.Print();
s1.BubbleSort();
s1.Print();
s2.Insert(6, 1);
s2.Print();
s3.PopBack();
s3.Print();
}
void Test1()
{
Vector<int> s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PushBack(6);
s1.Print();
s1.Delete(6);
s1.Print();
}