#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
线性表--链表描述
最新推荐文章于 2022-11-22 17:00:14 发布