C++模板类实现顺序表

时间:2022-06-05 19:51:16

C++模板类实现顺序表

#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
#pragma warning(disable:4996)

template<class T>
class Seqlist //定义一个顺序表的类
{
public:
Seqlist() //构造函数
:_a(NULL), _size(0), _capacity(0)
{}
Seqlist(const Seqlist<T> &s) //拷贝构造函数
{
_a = new T[s._size];
//memcpy(_a, s._a, sizeof(T)*s._size);
for (size_t i = 0; i < s._size; ++i)
{
_a[i] = s._a[i];
}
_size = s._size;
_capacity = s._size;
}
void Swap(Seqlist& s) //交换函数
{
swap(_a, s._a);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
Seqlist<T>& operator=(Seqlist<T> s) //赋值运算符的重载
{
this->Swap(s);
return *this;
}
~Seqlist() //析构函数
{
if (_a != NULL)
{
delete[] _a;
//free(_a);
_size = _capacity = 0;
}
}
void PushBack(const T& x) //尾插
{
CheekCapacity();
_a[_size] = x;
_size++;
}
void PushFront(const T& x) //头插
{
CheekCapacity();
for (size_t i = _size; i > 0; i--)
{
_a[i] = _a[i - 1];
}
_a[0] = x;
_size++;
}
void CheekCapacity() //检查容量及扩容函数
{
if (_size >= _capacity)
{
_capacity = _capacity * 2 + 3;
T* tmp = new T[_capacity];
if (_a != NULL)
{
//memcpy(tmp, _a, sizeof(T)*_size); 适用于内置类型
for (size_t i = 0; i < _size; ++i)
{
tmp[i] = _a[i];
}
delete[] _a;
}
_a = tmp;
}
}
void Print() //显示函数
{
for (size_t i = 0; i < _size; ++i)
{
cout << _a[i] << " ";
}
cout << endl;
}
void PopBack() //尾删函数
{
assert(_size>0);
--_size;
}

void PopFront() //头删
{
assert(_size > 0);
for (size_t i = 0; i < _size; i++)
{
_a[i] = _a[i + 1];
}
--_size;
}
void Insert(size_t pos, T x) //指定位置前插入函数
{
assert(pos > 0 && pos < _size);
for (size_t i = _size; i >= pos; i--)
{
_a[i] = _a[i - 1];
}
_a[pos - 1] = x;
_size++;
}
void Delete(size_t pos) //指定位置前删除函数
{
assert(pos>0 && pos < _size);
for (size_t i = pos - 1; i < _size; i++)
{
_a[i] = _a[i + 1];
}
_size--;
}
int Find(T x) //查找函数
{
for (size_t i = 0; i < _size; i++)
{
if (_a[i] == x)
{
return i + 1;
}
else
continue;
}
}
private:
T *_a;
size_t _size;
size_t _capacity;
};

void test1() //int类型数据的后插
{
Seqlist<int> s1;
s1.PushBack(1);
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.Print();
}
void test2() //字符串的后插
{
Seqlist<string> s2;
s2.PushBack("123");
s2.PushBack("456");
s2.PushBack("789");
s2.PushBack("qwertyuioplkjhgfdsa");
s2.Print();
}

void test3() //拷贝构造之int
{
Seqlist<int> s3;
s3.PushBack(1);
s3.PushBack(2);
s3.PushBack(3);
s3.PushBack(4);
Seqlist<int> s4(s3);
s3.Print();
s4.Print();
}

void test4() //拷贝构造之string字符串
{
Seqlist<string> s5;
s5.PushBack("123");
s5.PushBack("456");
s5.PushBack("789");
s5.PushBack("qwertyuioplkjhgfdsa");
s5.Print();
Seqlist<string> s6(s5);
s6.Print();
}

void test5() //尾删
{
Seqlist<string> s7;
s7.PushBack("123");
s7.PushBack("456");
s7.PushBack("789");
s7.PushBack("qwertyuioplkjhgfdsa");
s7.Print();
s7.PopBack();
s7.PopBack();
s7.PopBack();
s7.PopBack();
s7.Print();
}

void test6()//查找和指定位置之前的插入和删除指定位置之前的元素
{
Seqlist<int> s8;
s8.PushBack(1);
s8.PushBack(2);
s8.PushBack(3);
s8.PushBack(4);
s8.Print();
cout << s8.Find(1) << endl;
s8.Insert(1, 0);
s8.Print();
s8.Delete(1);
s8.Print();
}
void test7() //头插
{
Seqlist<int> s9;
s9.PushFront(1);
s9.PushFront(2);
s9.PushFront(3);
s9.PushFront(4);
s9.Print();
}
void test8() //头删
{
Seqlist<int> s10;
s10.PushFront(1);
s10.PushFront(2);
s10.PushFront(3);
s10.PushFront(4);
s10.Print();
s10.PopFront();
s10.PopFront();
s10.PopFront();
s10.PopFront();
s10.Print();
}

int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
//test6();
//test7();
test8();
system("pause");
return 0;
}

C++模板类实现顺序表

C++模板类实现顺序表

拷贝构造和上述图片中也一样,需要采用合适的方法,简单地说这里的memcpy拷贝适用于内置类型,不适合string。