我写了一个A.h头文件,里面包含节点LNode和线性链表LinkList的定义
#include<iostream.h>
#ifndef A_H
#define A_H
template<class T> class LinkList;
template<class T>
class LNode
{
friend class LinkList<T>;
public:
LNode();
LNode(const T & item);
LNode(const T & item,LNode<T> * Next);
~LNode();
LNode<T> * Next();
void InsertAfter(LNode<T> * p);
LNode<T> * GetNode(const T & item,LNode<T> * Next);
LNode<T> * RemoveAfter();
private:
T data;
LNode<T> * next;
};
template<class T>
class LinkList
{
public:
LinkList()
{
tail=head=new LNode<T>(0);
}
LinkList(const T & val)
{
tail=head=new LNode<T>(val);
}
~LinkList()
{
Empty();
delete head;
}
void AddToTail();
void Empty();
int Length() const;
LNode<T> * FindByVal(T val);
LNode<T> * FindByIndex(int i);
int Insert(T val,int i);
T * Remove(int i);
T * Get(int i);
void Print()
{
LNode<T> * temp=head->next;
cout<<"¹²ÓÐ"<<Length()<<"¸ö½Úµã"<<endl;
while(temp)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
private:
LNode<T> * tail;
LNode<T> * head;
};
#endif
之后写了一个实现其中所声明的函数的cpp文件A。cpp
#include"A.h"
template<class T>
LNode<T>::LNode(const T & item)
{
data=item;
next=NULL;
}
template<class T>
LNode<T>::LNode(const T & item,LNode<T> * Next)
{
data=item;
next=Next;
}
template<class T>
LNode<T>::~LNode()
{
}
template<class T>
LNode<T> * LNode<T>::Next()
{
return next;
}
template<class T>
void InsertAfter(LNode<T> * p)
{
p->next=next;
next=p;
}
template<class T>
LNode<T> * GetNode(const T & item,LNode<T> * Next)
{
LNode<T> * newNode=new LNode<T>(item);
newNode->next=Next;
return newNode;
}
template<class T>
LNode<T> * LNode<T>::RemoveAfter()
{
Lnode<T> * tempNodeptr=next;
if(next==NULL)
return NULL;
next=tempNodeptr->next;
return next;
}
template<class T>
void LinkList<T>::Empty()
{
LNode<T> temp;
while(head->next!=NULL)
{
temp=head->next;
head->next=temp->next;
delete temp;
}
}
template<class T>
int LinkList<T>::Length() const
{
int i=0;
while(head->next!=NULL)
i++;
return i;
}
template<class T>
LNode<T> * LinkList<T>::FindByVal(T val)
{
LNode<T> * q=head;
while(q->next!=NULL&&val!=q.data)
q=q->next;
if(q->next==NULL)
cout<<"Not Find!!!!!!"
return q;
}
template<class T>
LNode<T> * LinkList<T>::FindByIndex(int i)
{
int j;
LNode<T> * q=head;
for(j=0;j<i;j++)
q=q->next;
return q;
}
template<class T>
int LinkList<T>::Insert(T val,int i)
{
LNode<T> * q=FindByIndex(i);
LNode<T> * p;
p->next=q->next;
q->next=q;
return 1;
}
template<class T>
T * LinkList<T>::Remove(int i)
{
int j=0;
LNode<T> * q=head;
while(j<i-1)
{
q=q->next;
}
q->next=q->next->next;
return q->next;
}
template<class T>
T * LinkList<T>::Get(int i)
{
int j=0;
LNode<T>* q=head;
while(j<i)
{
q=q->next;
}
return q;
}
template<class T>
void LinkList<T>::AddToTail()
{
LNode * s=new LNode(0);
T x;
cin>>x;
while(x!='#')
{
s.data=x;;
tail->next=s;
s->next=tail;
cin>>x;
}
}
最后写了主文件对他们进行使用,结果
#include"A.h"
#include"A.cpp"
void main()
{
int len,x,i,j;
int * m;
LinkList<int> s();
s.AddToTail();
len=s.Length();
}
提示我AddToTail和Length:must have class/struct/union type
求高手指点一下