模板实现顺序表和单链表

模板实现顺序表
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
template<class T>
class SeqList
{
public:
	SeqList();
	SeqList(T*pData,size_t _size);
	SeqList(const SeqList<T>&s);
	SeqList<T>& operator=(const SeqList<T>&s);
	~SeqList();
public:
	void Pushback(const T& data);
	void Popback();
	void PushFront(const T& data);
	void PopFront();
	void Insert(size_t pos,const T& data);
	void All_Erase(const T& data);
	int Find(const T& data);
	void print()
	{
		for(int idx=0;idx<_size;idx++)
		{
			cout<<_pData[idx]<<" ";
		}
		cout<<endl;
	}
private:
	void Checkcapatity()
	{
		if(_size==_capatity)
		{
			_capatity=_capatity*2+3;
			_pData=(T*)realloc(_pData,_capatity*sizeof(T)+3);
		}
	}
	
			
	size_t _size;
	size_t _capatity;
	T* _pData;
};
template<class T>
SeqList<T>::SeqList()
	:_size(0)
	,_capatity(0)
	,_pData(NULL)
{}
template<class T>
SeqList<T>::SeqList(T*pData,size_t size)
	:_size(size)
	,_capatity(size)
	,_pData(new T[_capatity*sizeof(T)])
{
	for(int idx=0;idx<size;idx++)
	{
		_pData[idx]=pData[idx];
	}
}
template<class T>
SeqList<T>::SeqList(const SeqList<T>& s)
	:_size(s._size)
	,_capatity(s._capatity)
	,_pData(new T[_capatity])
{
	for(int idx=0;idx<s._size;idx++)
	{
		_pData[idx]=s._pData[idx];
	}
}
template<class T>
SeqList<T>& SeqList<T>::operator=(const SeqList<T>&s)
{
	SeqList<T> tmp(s._pData);
	swap(_pData,tmp._pData);
	_size=s._size;
	_capatity=s._capatity;
	return *this;
}
template<class T>
SeqList<T>::~SeqList()
{
	if(_pData!=NULL)
	{
		delete[]_pData;
	}
}
template<class T>
void SeqList<T>::Pushback(const T&data)
{
	Checkcapatity();
	_pData[_size++]=data;
}
template<class T>
void SeqList<T>::Popback()
{
	assert(_size>=0);
	--_size;
}
template<class T>
void SeqList<T>::PushFront(const T& data)
{
	Checkcapatity();
	for(int idx=_size-1;idx>=0;idx--)
	{
		_pData[idx+1]=_pData[idx];
	}
	_pData[0]=data;
	++_size;
}
template<class T>
void SeqList<T>::PopFront()
{
	assert(_size>=0);
	for(int idx=0;idx<_size-1;idx++)
	{
		_pData[idx]=_pData[idx+1];
	}
	--_size;
}
template<class T>
void SeqList<T>::Insert(size_t pos,const T&data)
{
	assert(pos<=_size);
	Checkcapatity();
	for(int idx=_size-1;idx>=pos;idx--)
	{
		_pData[idx+1]=_pData[idx];
	}
	_pData[pos]=data;
	++_size;
}
template<class T>
void SeqList<T>::All_Erase(const T& data)
{
	int count=0;
	assert(_size>0);
	for(int idx=0;idx<_size;idx++)
	{
		if(_pData[idx]==data)
		{
			count++;
		}
		else
		{
			_pData[idx-count]=_pData[idx];
		}
		
	}
	_size-=count;
}
template<class T>
int SeqList<T>::Find(const T&data)
{
	assert(_size>0);
	for(int idx=0;idx<_size;idx++)
	{
		if(_pData[idx]==data)
		{
			return idx;
		}
	}
	return -1;
}
void main()
{
	int arr[]={1,2,3,4,5,6,7,8,2};
	SeqList<int> s1(arr,8);
	SeqList<int> s2(s1);
	/*s1.Pushback(9);*/
	/*s1.Popback();*/
	/*s1.PushFront(0);*/
	/*s1.PopFront();*/
	///*s1.Insert(3,10);*/
	s1.All_Erase(2);
	s1.print();
	s2.print();
	system("pause");
	
}

//模板实现单链表
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
template<class T>
 struct LinkNode
{
	T _data;
	LinkNode* next;
	LinkNode(const T&d)
		:_data(d)
		,next(NULL)
	{}

};
 template<class T>
 class LinkList
 {
 public:
	 LinkList()
		 :_head(NULL)
		 ,_tail(NULL)
	 {}
	 ~LinkList()
	 {
		 if(_head==NULL)
		 {}
		 else if((_head==_tail)&&(_head!=NULL))
		 {
			 delete _head;
			 _head=_tail=NULL;
		 }
		 else
		 {
			 while(_head)
			 {
				 LinkNode<T>*cur=_head;
				 LinkNode<T>*del=NULL;
				 {
					 del=_head;
					 _head=_head->next;
					 delete _tail;
				 }
			 }
		 }
	 }
	 public:
		 void PushBack(const T&d);
		 void PopBack();
		 void PushFront(const T&d);
		 void PopFront();
		 void Display();
		 int listlen();
		 void Insert(const int&pos,const T&d);
		 void Erase(const int&pos);
	 private:
		 LinkNode<T>* _head;
		 LinkNode<T>* _tail;
 };
 template<class T>
 void LinkList<T>::PushBack(const T&d)
 {
	 if(_head==NULL)
	 {
		 _head=new LinkNode<T>(d);
		 _tail=_head;
	 }
	 else
	 {
		 LinkNode<T>*cur=_head;
		 LinkNode<T>*p=new LinkNode<T>(d);
		 while(cur->next!=NULL)
		 {
			 cur=cur->next;
		 }
		 cur->next=p;
		 _tail=p;
		 p->next=NULL;
	 }
 }
 template<class T>
 void LinkList<T>::PopBack()
 {
	 if(_head==NULL)
	 {
		 return ;
	 }
	 else if((_head==_tail)&&(_head!=NULL))
	 {
		 delete _head;
		 _head=_tail=NULL;
	 }
	 else 
	 {
		 LinkNode<T>*cur=_head;
		 while(cur->next!=NULL)
		 {
			 _tail=cur;
			 cur=cur->next;
		 }
		 delete cur;
		 _tail->next=NULL;
	 }
 }
 template<class T>
 void LinkList<T>::PushFront(const T&d)
 {
	 if(_head==NULL)
	 {
		 _head=new LinkNode<T>(d);
		 _tail=_head;
	 }
	 else
	 {
		 LinkNode<T>* cur=new LinkNode<T>(d);
		 cur->next=_head;
		 _head=cur;
	 }

 }
 template<class T>
 void LinkList<T>::PopFront()
 {
	 if(_head==NULL)
	 {
		 return ;
	 }
	 if((_head==_tail)&&(_head!=NULL))
	 {
		 delete _head;
		 _head=_tail=NULL;
	 }
	 else
	 {
		 LinkNode<T>* cur=_head;
		 _head=_head->next;
		 free(cur);
	 }
 }
 template<class T>
 void LinkList<T>::Display()
 {
	 LinkNode<T>*cur=_head;
	 while(cur)
	 {
		 cout<<cur->_data<<" ";
		 cur=cur->next;
	 }
	 cout<<endl;
 }
 template<class T>
 int LinkList<T>::listlen()
 {
	 int count=0;
	 LinkNode<T>*cur=_head;
	 while(cur)
	 {
		 cur=cur->next;
		 count++;
	 }
	 return count;
 }
 template<class T>
 void LinkList<T>::Insert(const int& pos,const T&d)
 {
	 int num=pos;
	 if(num>listlen()+1)
	 {
		 return ;
	 }
	 else
	 {
		 LinkNode<T>*cur=_head;
		 LinkNode<T>*p1=NULL;
		 LinkNode<T>*p=new LinkNode<T>(d);
		  if(num==1)
		 {
			 p->next=_head;
			 _head=p;
		  }
		  else
		  {
			  while(--num)
			  {
				  p1=cur;
				  cur=cur->next;
			  }
			  p->next=cur;
			  p1->next=p;
		  }
	 }
 }
 template<class T>
 void LinkList<T>::Erase(const int& pos)
 { 
	 int num=pos;
	 if(_head==NULL)
	 {
		 return ;
	 }
	 else if((_head==_tail)&&(_head!=NULL))
	 {
		 delete _head;
		 _head=_tail=NULL;
	 }
	 else
	 {
		 LinkNode<T>*cur=_head;
		 
		 if(num==1)
		 {
			 _head=_head->next;
			 delete cur;
		 }
		 else
		 {
			 LinkNode<T>* tmp1=_head;
			 LinkNode<T>* tmp2=_head;
			 while(--num)
			 {
				 tmp2=tmp1;
				 tmp1=tmp1->next;
			 }
			 tmp2->next=tmp1->next;
			 delete tmp1;
		 }
	 }
 }
 void main()
 {
	 LinkList<int>l1;
	 l1.PushBack(1);
	 l1.PushBack(2);
	 l1.PushBack(3);
	 l1.PushBack(4);
	 l1.PushBack(5);
	 /*l1.PopFront();*/
	 /*l1.PopBack();*/
	 /*l1.PushFront(0);*/
	 /*l1.Insert(2,8);*/
	 l1.Erase(2);
	 l1.Display();
	 system("pause");
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值