线性表--链表描述

#ifndef  _LIST_H_
#define  _LIST_H_

#include<iostream>

/*线性表的抽象类*/
template<class T>
class LineList
{
public:
	 virtual ~LineList(){};  
	 virtual bool empty() const =0;           //判断线性表是否为空
	 virtual int size() const =0;             //获取线性表长度
	 virtual T& get(int theIndex) const =0;   //获取索引为theIndex元素
	 virtual int intdexOf(const T& theElement) const =0; //返回元素为theElement元素的索引
	 virtual void erase(int intdexOf) const =0;          //删除索引为indexof的元素
	 virtual void insert(int intdexOf,T& theElement) const =0; //在索引indexof插入元素theElement
	 virtual void output(std::ostream& out) const =0;            //把线性表元素插入到out输出流
};
/*链表节点类*/
template<class T>
class chainNode
{
private:
	 T element;               //数据域
	 chainNode<T> *next;      //指针域
	 chainNode() {};
	 chainNode(const T& element)
	 {
	     this->element=element;
	 }
	 chainNode(const T& element,chainNode<T>* next)
	 {
	     this->element=element;
		 this->next=next;
	 }
};
/*链表类*/
template<class T>
class chain :public LineList<T>
{
protected:
	chainNode<T>* firstNode;
	int listSize;
	void CheckIndex(int theIndex) const;
public:
	chain(int initcapacity =10);
	chain(const chain<T>&);
	~chain();
	/*抽象数据类型ADT的方法*/
	bool empty() const;
	int size() const;
	T& get(int theIndex) const;
	int indexOf(const T& theElement) const;
	void erase(int theIndex) const;
	void insert(int theIndex,const T& theElement)const;
	void output(std::ostream& out) const;
};

template<class T>
chain<T>::chain(int initcapacity)
{
	if(initcapacity<1)
	{
	    throw "capacity must > 0";
	}
	firstNode = NULL;
	listSize=0;
}
template<class T>
chain<T>::chain(const chain<T>& theList)
{
	listSize = theList.listSize;
	if(listSize ==0)
	{
		firstNode=NULL;
		return ;
	}
	chainNode<T>* sourceNode= theList.firstNode;
	firstNode = new chainNode<T>(sourceNode->element);
	sourceNode = sourceNode->next;
	chainNode<T>* targetNode= firstNode;
	while(sourceNode!= NULL)
	{ 
		targetNode->next=new chainNode<T>(sourceNode->element);
		targetNode = targetNode->next;
		sourceNode = sourceNode->next;
	}
	targetNode->next=NULL;
}

template<class T>
chain<T>::~chain()
{
	while(firstNode !=NULL)
	{
		chainNode<T>* nextNode=firstNode->next;
		delete firstNode;
		firstNode= nextNode; 
	}
}
template<class T>

void chain<T>::CheckIndex(int theIndex) const
{
	if(theIndex<0 || theIndex >= listSize)
	{
	  throw "theIndex error";
	}
}
template<class T>
bool chain<T>::empty() const
{
	return listSize==0;
}

template<class T>
int chain<T>::size() const
{
    return listSize;
}
template<class T>
T& chain<T>::get(int theIndex) const
{
	CheckIndex(theIndex); 
	chainNode<T>* currentNode = firstNode;

	for(int i=0;i<theIndex;i++)
	{
		currentNode=currentNode->next;
	}
	return currentNode->element;
}
template<class T>
int chain<T>::indexOf(const T& theElement) const
{
	chainNode<T>* currentnode = firstNode;
	int index=0;
	while(currentnode != NULL && currentnode->element != theElement)
	{
		currentnode= currentnode->next;
		index++
	}
	if(currentnode == NULL)
	{
	    return -1;
	}
	else
	{
	  return index;
	}
}
template<class T>
void chain<T>::erase(int theIndex) const 
{
   CheckIndex(theIndex);

   chainNode<T>* deleteNode;
   if(theIndex==0)
   {
	   deleteNode= firstNode;
	   firstNode=firstNode->next;
   }
   else
   {
	   chainNode<T>* p=firstNode;
	   for(int i=0;i<theIndex-1;i++)
	   {
		   p=p->next;
	   }
	   deleteNode=p->next;
	   p->next=p->next->next;
    }
    listSize--;
    delete deleteNode;
}

template<class T>
void chain<T>::insert(int theIndex,const T& theElement) const
{
	CheckIndex(theIndex);
	if(theIndex ==0)
	{
		firstNode = new chainNode<T>(theElement,firstNode);
	}
	else
	{
		chainNode<T>* p = firstNode;
		for(int i=0;i<theIndex-1;i++)
		{
			p=p->next;
		}
		p->next=new chainNode<T>(theElement,p->next);
	}
	listSize++;
}

template<class T>
void chain<T>::output(std::ostream& out) const
{
	for(chainNode<T>* currentNode=firstNode; currentNode!=NULL;currentNode=currentNode->next)
	{
		std::cout<<currentNode->element<<" ";
	
	}
	std::cout<<endl;
}
template<class T>
std::ostream& operator <<(std::ostream& out,const chain<T>& x)
{
	x.output(out);
	return out;
}
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值