顺序表模板

#pragma once
#include<iostream>
#include<cstring>
#include<string>
#include<cassert>
using namespace std;

template<typename T>
class Seqlist
{
public:
	Seqlist();
	Seqlist(const Seqlist<T>& seq);
	Seqlist & operator=(Seqlist<T> seq);
	~Seqlist();
	void PushBack(const T& d);
	void PushFront(const T& d);
	void PopBack();
	void PopFront();
	void Insert(int index,const T& d);
	int Search(const T& d);
	void Remove(const T& d);
	void RemoveAll(const T& d);
	void Sort();
	void Reserve(int n);
	void Display();
private:
	void CheckCapacity(int n=0);
private:
	T *_pdata;
	int _sz;
	int _capacity;
};

template<typename T>
Seqlist<T>::Seqlist()
             :_sz(0)
              , _capacity(0)
			  , _pdata(NULL){}

template<typename T>
Seqlist<T>::Seqlist(const Seqlist<T>& seq)
{

	_pdata = new T[seq._capacity];
	for (int i = 0; i < seq._sz; i++)
	{
		_pdata[i] = seq._pdata[i];
	}
	_sz = seq._sz;
	_capacity = seq._capacity;
}


template<typename T>
Seqlist<T>& Seqlist<T>::operator=(Seqlist<T> seq)
{
	swap(_pdata,seq._pdata);
	_sz = seq._sz;
	_capacity = seq._capacity;
	return *this;
}

template<typename T>
Seqlist<T>::~Seqlist()
{
    if (_pdata != NULL)
	{
		delete[] _pdata;
		_pdata = NULL;
		_sz = 0;
		_capacity = 0;
	}
}

template<typename T>
void Seqlist<T>::CheckCapacity(int n=0)
{
	if (_sz == _capacity||n>_capacity)
	{
		int NewCapacity =2*_capacity+1;
		if (n > _capacity)
		{
			NewCapacity = n;
		}
		T* tmp = new T[NewCapacity];
		for (int i = 0; i < _sz; i++)
		{
			tmp[i] =_pdata[i];
		}
		delete[] _pdata;
		_pdata = tmp;
		_capacity = NewCapacity;
	}

}

template<typename T>
void Seqlist<T>::PushBack(const T& d)
{
	CheckCapacity();
	_pdata[_sz++] = d;
}

template<typename T>
void Seqlist<T>::PushFront(const T& d)
{
	CheckCapacity();
	//memmove(_pdata + 1,_pdata,sizeof(T)*_sz);
	for (int i =_sz;i>0; i--)
	{
		_pdata[i] = _pdata[i-1];
	}
	_pdata[0] = d;
	_sz++;
}

template<typename T>
void Seqlist<T>::PopBack()
{
	_sz--;
}

template<typename T>
void Seqlist<T>::PopFront()
{
	//memmove(_pdata,_pdata+1,sizeof(T)*(--_sz));
	for (int i = 0; i<_sz-1; i++)
	{
		_pdata[i] = _pdata[i+1];
	}
	_sz--;
}

template<typename T>
void Seqlist<T>::Insert(int index, const T& d)
{
	assert(index >= 0);
	assert(index<_sz);
	CheckCapacity();
	//memmove(_pdata+index+1,_pdata+index,sizeof(T)*(_sz-index));
	for (int i = _sz; i>index; i--)
	{
		_pdata[i] = _pdata[i - 1];
	}
	_sz++;
	_pdata[index] = d;
}

template<typename T>
int Seqlist<T>::Search(const T& d)
{
	int i = 0;
	for (i = 0; i < _sz; i++)
	{
		if (_pdata[i] == d)
		{
			return i;
		}
	}
	return -1;              //没找到返回-1
}

template<typename T>
void Seqlist<T>::Remove(const T& d)
{
	int index = Search(d);
	if (index == -1)
	{
		return;
	}
	else
	{
		//memmove(_pdata+index,_pdata+index+1,sizeof(T)*(_sz-index-1));
		for (int i =index; i<_sz-1; i++)
		{
			_pdata[i] = _pdata[i+1];
		}
		_sz--;
	}
}

template<typename T>
void Seqlist<T>::RemoveAll(const T& d)
{
	while (Search(d) != -1)
	{
		Remove(d);
	}
}

template<typename T>
void Seqlist<T>::Reserve(int n)
{
	CheckCapacity(n);
}

template<typename T>
void Seqlist<T>::Sort()
{
	int end = _sz - 1;
	for (int i = 0; i < _sz - 1; i++)
	{
		int flag = 1;
		int k = end;
		for (int j = 0; j <end;j++)
		{
			if (_pdata[j]>_pdata[j+1])
			{
				T tmp = _pdata[j];
				_pdata[j] = _pdata[j + 1];
				_pdata[j + 1] = tmp;
				flag = 0;
				k= j;
			}
		}
		if (flag == 1)
		{
			return;
		}
		end = k;
	}
}


template<typename T>
void Seqlist<T>::Display()
{
	for (int i = 0; i < _sz; i++)
	{
		cout << _pdata[i] << " ";
	}
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值