求大侠们帮我看看这个程序,运行老出错!!!
/////////////////////////////////////////////////////////////////////////////////////////
///////////// 单链表类的实现 ///////////
//////////// 日期:2005-5-18 ///////////
/////////////////////////////////////////////////////////////////////////////////////////
template <class Type> class List; //前视的类定义
template <class Type> class ListNode //链表节点类的定义
{
public:
friend class List<Type>; //List 类作为友元类定义
ListNode(); //无参构造函数
ListNode(const Type& item); //拷贝构造函数
ListNode<Type>* NextNode() {return link;} //给出当前节点的下一个节点的地址
void InsertAfter(ListNode<Type> *p); //当前节点插入
ListNode<Type>* GetNode(const Type& item,ListNode<Type> *next); //建立一个新节点
ListNode<Type> * RemoveAfter(); //删除一个节点
/*ListNode<Type> operator=(const ListNode<Type>* other)
{
if(this==other)
return this;*/
private:
Type data; //数据
ListNode<Type> * link; //指针
};
template <class Type> class List //单链表类定义
{
public:
List(const Type& value) { last=first=new ListNode<Type> (value);}//构造函数
~List(); //析构函数
void MakeEmpty(); //链表置空
int Length() const ; //取得链表长度
ListNode<Type>* Find(Type value); //在链表中搜索含value的节点
ListNode<Type> *Find(int i); //搜索链表中第i元素
int Insert(Type value,int i); //将新元素插入链表中第I个位置
Type* Get(int i); // 取出链表中第i 个元素
Type* Remove(int i); //删去链表中第i 个元素
private:
ListNode<Type> *first,*last;
};
template <class Type> ListNode<Type>::ListNode():link(NULL){} //构造函数仅初始化指针成员
template <class Type> ListNode<Type>::ListNode(const Type& item):data(item),link(NULL){}//构造函数初始化数据和指针
template <class Type> void ListNode<Type>::InsertAfter(ListNode<Type>* p)
{
p->link=link;
link=p;
} //将p所指的接点连接为当前接点的后继
template <class Type> ListNode<Type>* ListNode<Type>::GetNode(const Type& item,ListNode<Type>* next)
{
ListNode<Type> *newnode=new ListNode<Type>(item);
newnode->link=next;
return newnode;
} //以数据成员item和指针next为参数,建立一个新节点,函数返回新节点地址
template <class Type> ListNode<Type> *ListNode<Type>::RemoveAfter(){
ListNode<Type>* tempptr=link;
if(link==NULL)
return NULL;
link=tempptr->link;
return temptr;
} //从链表中摘下当前节点的下一个节点,并为删除他而返回其地址
template <class Type> List<Type>::~List()
{
MakeEmpty();
delete first;
} //析构函数
template <class Type> void List<Type>::MakeEmpty()
{
ListNode<Type> * q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
last=first;
} //将链表置空
template <class Type> int List<Type>::Length()const
{
ListNode<Type>* p=first->link;
int count=0;
while(p!=NULL)
{
p=p->link;
count++;
}
return count;
} //计算链表长度
template <class Type> ListNode<Type>* List<Type>::Find(Type value)
{
ListNode<Type>* p=first->link;
while(p!=NULL&&p.data!=value)
p=p->link;
return p;
} //搜索接点等于value的节点
template <class Type> ListNode<Type>* List<Type>::Find(int i)
{
if(i<-1)
return NULL;
if(i==-1)
return first;
ListNode<Type>* p=first->link;
int j=0;
while(p!=NULL&&j<i)
{
p=p->link;
j++;
}
return p;
} //定位函数
template <class Type> int List<Type>::Insert(Type value,int i)
{
ListNode<Type> *p=Find(i-1);
if(p=NULL)
return 0;
ListNode<Type>* newnode=GetNode(value,p->link);
if(p->link=NULL)
last=newnode;
p->link=newnode;
return 1;
} //将value插入链表中第i个位置
template <class Type> Type* List<Type>::Remove(int i)
{
ListNode<Type>* p=Find(i-1),*q;
if(p=NULL&&p->link=NULL)
return NULL;
q=p->link;
p->link=q->link;
Type value=new Type(q->data);
if(q==last)
last=p;
delete q;
return &value;
} //删除链表中的第i个元素
template <class Type> Type* List<Type>::Get(int i)
{
ListNode<Type> *p=Find(i);
if(p==NULL||p==first)
return NULL;
else
return &p->data;
} //取出链表中第i个元素
#include "iostream.h"
#include "singlist.h"
// 测试程序主程序
void main()
{
List<float> ls(0.0);
int j=0;
float c;
cin>>c;
while(c!=-1)
{
ls.Insert(c,j);
j++;
cin>>c;
}
int n=ls.Length();
cout<<"The length of the list is %d"<<n<<endl;
}