c++实现顺序表
程序员文章站
2024-03-23 12:32:34
...
#include<iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
class SeqList
{
public:
SeqList()
: _array(new DataType[3]) //初始化列表
, _capacity(3)
, _size(0)
{}
Seqlist(DataType* array,size_t size)//构造函数
:_array(new DataType[size])
,_size(size)
,_capacity(_size)
{
for(size_t i=0;i<size;i++)
{
_array[i]=array[i];
}
}
SeqList(SeqList &s)//拷贝构造函数
{
if (s._array == NULL)
return;
DataType *tmp = new DataType[s._size];
memcpy(tmp, s._array, s._size*sizeof(DataType));
delete[]_array;
_array = tmp;
_size = s._size;
_capacity = _size;
}
//:_array(new DataType[s._size])
//, _size(s._size)
//, _capacity(s._size)
//{
memcpy(_array, s._array, sizeof(DataType)*_size);
//}
SeqList&operator=(const SeqList&s)//赋值运算符重载
{
if (this != &s)
{
DataType *temp = new DataType[s._size];
delete[]_array;
_array = temp;
_size = s._size;
_capacity = s._size;
memcpy(_array, s._array, sizeof(DataType)*_size);
}
return *this;
}
~SeqList()//析构函数
{
if (_array)
{
delete[] _array;
_size = 0;
_capacity = 0;
}
}
void pushback(DataType data)//尾插函数
{
_Checkcapacity();
_array[_size++] = data;
}
void popback(DataType data)//尾删函数
{
if (_array)
{
_array[_size--];
}
}
void pushFront(DataType data)//头插函数
{
if (_size == _capacity)
_Checkcapacity();
int i = _size - 1;
for (i = _size - 1; i >= 0; i--)
{
_array[i + 1] = _array[i];
}
_array[0] = data;
_size++;
}
void popFront(DataType data)//头插函数
{
if (_array)
{
for (int i = 0; i<_size - 1; i++)
{
_array[i] = _array[i + 1];
}
_size--;
}
}
void Insert(size_t pos, DataType data)
{
assert(pos <= _size);
_Checkcapacity();
for (int i = _size - 1; i >= pos; i--)
{
_array[i + 1] = _array[i];
}
_array[pos] = data;
_size++;
}
void Erase(size_t pos)
{
assert(pos < _size);
for (int i = pos; i < (_size - 1); i++)
{
_array[i] = _array[i + 1];
_size--;
}
}
size_t Size()const
{
return _size;
}
size_t Capacity()const
{
return _capacity;
}
bool Empty()const
{
return 0 == _size;
}
DataType& operator[](size_t index)//数组索引
{
return _array[index];
}
const DataType& operator[](size_t index)const
{
return _array[index];
}
// 返回顺序表中的第一个元素
DataType& Front()
{
return _array[0];
}
const DataType& Front()const
{
return _array[0];
}
// 返回顺序表中最后一个元素
DataType& Back()
{
return _array[_size - 1];
}
const DataType& Back()const{
return _array[_size - 1];
}
void Clear()
{
_size = 0;
}
void reserve() //逆序
{
assert(_array);
int left = 0;
int right = _size - 1;
while (left<right)
{
DataType k = _array[left];
_array[left] = _array[right];
_array[right] = k;
left++;
right--;
}
}
void display() //显示函数
{
for (size_t i = 0; i < _size; ++i)
{
cout << _array[i] << " ";
}
cout << endl;
}
void ReSize(size_t newSize, const DataType& data = DataType())// 将顺序表中元素个数改变到newSize :分三种情况而定《1.newSize <= _size 2.(_size<newSize) && (_capacity>newSize) 3._capacity<newSize》
{
if (newSize <= _size)
{
_size = newSize;
}
else if((_size<newSize) && (_capacity>newSize))
{
for (size_t i = _size; i < newSize; ++i)
{
_array[i] = data;
_size = _capacity;
}
}
else
{
DataType*temp = new DataType[newSize];
for (size_t i = 0; i < _size; ++i)
{
temp[i] = _array[i];
}
for (size_t i = _size; i < newSize; ++i)
{
temp[i] = data;
}
delete[]_array;
_array = temp;
_size = newSize;
_capacity = newSize;
}
}
friend ostream& operator<<(ostream& _cout, const SeqList& s);//输出运算符重载
private:
DataType* _array;
size_t _size;
size_t _capacity;
void _Checkcapacity()
{
if (_size == _capacity)
{
DataType*temp = new DataType[(_capacity << 1)];//开辟新空间
for (size_t i = 0; i <_size; i++)
temp[i] = _array[i];
delete[]_array;
_array = temp;
_capacity = (_capacity << 1);
}
}
};
ostream& operator<<(ostream& os, const SeqList& s)
{
int i = 0;
for (i = 0; i<(s._size); i++)
{
os << s._array[i] << ' ';
}
os << endl;
return os;
}
void test1()
{
SeqList s1;
s1.pushback(1);
s1.pushback(2);
s1.pushback(3);
s1.pushback(4);
s1.display();
}
void test2()
{
SeqList s2;
s2.pushback(1);
s2.pushback(2);
s2.pushback(3);
s2.pushback(4);
SeqList s3=s2;
s2.display();
s3.display();
}
void test3()
{
SeqList s4;
s4.pushback(1);
s4.pushback(3);
s4.pushback(4);
s4.pushback(5);
s4.pushback(6);
s4.Insert(1, 1);
s4.display();
}
void test4()
{
SeqList s5;
s5.pushback(1);
s5.pushback(3);
s5.pushback(4);
s5.pushback(5);
s5.pushback(6);
//s5.Erase(2);
s5.ReSize(8, 8);
s5.display();
}
int main()
{
test1();
test2();
test3();
test4();
system("pause");
return 0;
}
具体运行结果如下图所示:
本人是初学者,希望大家给予建议
上一篇: java面试必备之ThreadLocal
下一篇: 详解链表的创建,插入,删除,遍历