這個simplelist。h是怎麼搞的?不太明白?
#if !defined_LIST_H_
#define LIST_H
//單鏈表定義
#include
#include"simplelist.h"
int main()
{
List l1;
cout<<"以-1作為約定的鏈表數據輸入結束符號"<
cout
l1.inputFrond(-1);
l1.output();
List l2(l1);
l2.output();
return 0;
}
template //鏈表節點類的定義
struct LinkNode
{
T date; //數據域
LinkNode*link; //鏈指針域
LinkNode(LinkNode *ptr =NULL) {link=ptr;}
LinkNode(const T&item,LinkNode *ptr=NULL)
{
data=item;
link=ptr;
}
};
template
class List
{
public:
List(){first=new LinkNode;}
List(const T &x){first= new LinkNode(x);}
List(List &L);
~List(){makeEmpty();}
void makeEmpty();
int Length() const;
LinkNode *getHead()const{return first;}
LinkNode *Search(T x);
LinkNode *Locate(int i);
bool getData(int i,T &x)const;
void setData(int i,T &x);
bool Insert(int i, T &x);
bool Remove(int i, T &x);
bool IsEmpty()const
{
return first->link==NULL?true:false;
}
bool IsFull()const{return false ;}
void Sort()
void inputFrond (T endTag);
void output();
List<T>&operator=(List <T>&L);
protected:
LinkNode<T> *first;
};
template
List::List(List &L)
{
T value;
LinkNode *srcptr=L.getHead();
LinkNode *desptr=first=new LinkNode;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new LinkNode(value);
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}
template
int List::makeEmpty()
{
LinkNode *q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
}
template
int List::Length()const
{
LinkNode *p=first->link;
int count=0;
while(p!=NULL)
{
p=p->link;
count++
}
return count;
}
template
LinkNode*List::Search(T x)
{
LinkNode*current=first->link;
while(current!=NULL)
if(current->data==x)break;
else current =current->link;
return current;
}
template
LinkNode*List::Loate(int i)
{
if(i<0)
{
return NULL;
}
LinkNode<T>*current =first;
int k=0;
while(current!=NULL&&k<i)
{
current=current->link;
k++;
}
return current;
}
template
bool List::getData(int i,T &x)const
{
if(i
LinkNode *current=Locate(i);
if(current==NULL) return false ;
else
{
x=current->data;
return true;
}
}
template
void List::setData(int i,T &x)
{
if(i<=0) return;
LinkNode *current =Locate(i);
if(current==NULL) return;
else current->data=x;
}
template
bool List::Insert(int i,T &x)
{
LinkNode *current=Locate(i);
if(current==NULL) return false;
LinkNode *newNode=new LinkNode(x);
if(newNode==NULL){return false;}
newNode->link=current->link;
current->link=newNode;
return true;
}
template
bool List::Remove(int i,T &x)
{
LinkNode*current=Locate(i-1);
if(current==NULL||current->link==NULL) return false;
LinkNode *del=current->link;
current->link=del->link;
x=del->data;
delete del;
return true;
}
template
void List::output()
{
LinkNode *current=first->link;
while(current!=NULL)
{
cout<data<
current=current->link;
}
}
template
void List::inputFront(T endTag)
{
LinkNode *newNode;
T val;
//MakeEmpty();
cin>>val;
while(val!=endTag)
{
newNode=new LinkNode(val);
if(newNode==NULL)
{
cerr<<"儲存分配錯誤"<
}
newNode->link=first->link;//插在表前端
first->link=newNode;
cin>>val;
}
}
template
List &List::operator=(List &L)
{
LinkNode *srcptr =L.getHead();
LinkNode *desptr =first=new LinkNode;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr=desptr->link;
srcptr=srcptr->link;
}
desptr->link=NULL;
}
#endif
挺長的,具體哪裡不明白?如果都不明白,說來就話長了。