采用类模板 完成链表
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
template<class ElemType>
struct Node
{
ElemType data;
Node<ElemType>*next;
Node();
Node(ElemType item,Node<ElemType>*link);
};
template<class ElemType>
Node<ElemType>::Node()
{
next=NULL;
}
template<class ElemType>
Node<ElemType>::Node(ElemType item,Node<ElemType> *link)
{
data=item;
next=link;
}
template<class ElemType>
class SimpleLinkList
{
protected:
Node<ElemType> *head;
Node<ElemType> *GetElemPtr(int position) const;
public:
SimpleLinkList();
~SimpleLinkList();
int Length() const;
bool Empty() const;
void Clear();
void Traverse() const;
bool GetElem(int position,ElemType &e) const;
bool SetElem(int position,const ElemType &e);
bool Delete(int position,ElemType &e);
bool Insert(int position,const ElemType &e);
};
template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position) const
{
Node<ElemType> *tmpPtr=head;
int pos=0;
while(tmpPtr!=NULL&&pos<position)
{
tmpPtr=tmpPtr->next;
pos++;
}
/*
if(tmpPtr!=NULL&&pos==position)
{
return tmpPtr;
}
else
{
return NULL;
}
*/
return tmpPtr;
}
template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList()
{
head=new Node<ElemType>;
}
template<class ElemType>
SimpleLinkList<ElemType>::~SimpleLinkList()
{
Clear();
delete head;
}
template<class ElemType>
int SimpleLinkList<ElemType>::Length() const
{
int cnt=0;
for(Node<ElemType> *tmpPtr=head->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
cnt++;
}
return cnt;
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Empty() const
{
return head->next=NULL;
}
template<class ElemType>
void SimpleLinkList<ElemType>::Clear()
{
ElemType tmpElem;
while(!Empty())
{
Delete(1,tmpElem);
}
}
template<class ElemType>
void SimpleLinkList<ElemType>::Traverse() const
{
Node<ElemType> *tmpPtr=head->next;
while(tmpPtr!=NULL)
{
printf("%d ",tmpPtr->data);
tmpPtr=tmpPtr->next;
}
printf("\n");
return;
}
template<class ElemType>
bool SimpleLinkList<ElemType>::GetElem(int position,ElemType &e) const
{
if(position<1||position>Length())
{
return false;
}
else
{
Node<ElemType>* tmpPtr;
tmpPtr=GetElemPtr(position);
e=tmpPtr->data;
return true;
}
}
template<class ElemType>
bool SimpleLinkList<ElemType>::SetElem(int position,const ElemType &e)
{
if(position<1||position>Length())
{
return false;
}
else
{
Node<ElemType> *tmpPtr;
tmpPtr=GetElemPtr(position);
tmpPtr->data=e;
return true;
}
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Insert(int position,const ElemType &e)
{
if(position<1||position>Length()+1)
{
return false;
}
else
{
Node<ElemType> *tmpPtr;
tmpPtr=GetElemPtr(position-1);
Node<ElemType> *newPtr;
newPtr=new Node<ElemType>(e,tmpPtr->next);
tmpPtr->next=newPtr;
return true;
}
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position,ElemType &e)
{
if(position<1||position>Length())
{
return false;
}
else
{
Node<ElemType> *tmpPtr;
tmpPtr=GetElemPtr(position-1);
Node<ElemType> *nextPtr=tmpPtr->next;
tmpPtr->next=nextPtr->next;
e=nextPtr->data;
delete nextPtr;
return true;
}
}
int main()
{
SimpleLinkList<int>s;
int a=5;
s.Insert(1,a);
s.Traverse();
return 0;
}