单链表(C++) (二)

2014-11-24 03:30:38 · 作者: · 浏览: 1
Find(Type value,int n);
ListNode *Find(int n);
bool Insert(Type item,int n=0);
Type Remove(int n=0);
bool RemoveAll(Type item);
Type Get(int n);
void Print();
};

//功能函数的实现

template void SingleList::MakeEmpty()
{
ListNode *pdel;
while(head->pnext!=null)
{
pdel=head->pnext;
head->pnext=pdel->pext;
delete pdel;
}
}

template int SingleList::Length()
{
ListNode *pmove=head->pnext;
int count=0;
while(pmove!=null)
{
pmove=pmove->pnext;
count++;
}
return count;
}

template ListNode* SingleList::Find(int n)
{
if(n<0)
{
cout<<"The N is out of boundry"< return null;
}
ListNode *pmove=head->pnext;
for(int i=0;i {
pmove=pmove->pnext;
}
if(pmove==null)
{
cout<<"The N is out of boundary"< return null;
}
}

template ListNode* SingleList::Find(Type value,int n){
if(n<1){
cout<<"The n is illegal"< return NULL;
}
ListNode *pmove=head;
int count=0;
while(count!=n&&pmove){
pmove=pmove->pnext;
if(pmove->data==value){
count++;
}

}
if(pmove==NULL){
cout<<"can't find the element"< return NULL;
}
return pmove;
}
template bool SingleList::Insert(Type item, int n){
if(n<0){
cout<<"The n is illegal"< return 0;
}
ListNode *pmove=head;
ListNode *pnode=new ListNode(item);
if(pnode==NULL){
cout<<"Application error!"< return 0;
}
for(int i=0;i pmove=pmove->pnext;
}
if(pmove==null){
cout<<"the n is illegal"< return 0;
}
pnode->pnext=pmove->pnext;
pmove->pnext=pnode;
return 1;
}

#endif