请教清华大学数据结构(黄皮书)中的程序问题!在线等!
我在学习这本书的链表一章中碰到这个编译错误,我是用VC++6.0编译的。
代码如下:
#ifndef LIST_NODE_H //链表节点类
#define LIST_NODE_H
#endif
#ifndef NULL
#define NULL 0
#endif
template <class Type> class List;
template <class Type> class ListNode{
friend class List<Type>;
public:
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();
private:
Type data;
ListNode <Type> *link;
};
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;
}
template <class Type> ListNode<Type> *ListNode<Type>::GetNode(const Type&item,ListNode<Type>*next=NULL){
ListNode<Type> *newnode=new ListNode<Type>(item);
newnode->link=next;
return newnode;
}
template <class Type> ListNode<Type> *ListNode<Type>::RemoveAfter(){
ListNode<Type> *tempptr=link;
if(link==NULL) return NULL;
link=tempptr->link;
return tempptr;
}
#ifndef LIST_H //链表类
#define LIST_H
#endif
#include "ListNode.h"
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);
ListNode <Type> *Find(int i);
int Insert(const Type value,int i);
Type *Remove(int i);
Type *Get(int i);
private:
ListNode <Type> *first,*last;
};
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;
}
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=j++;
}
return p;
}
template <class Type> int List<Type>::Insert(const Type value,int i){
ListNode<Type> *p=Find(i-1);
if(p==NULL) return 0;
ListNode <Type> *newnode=ListNode <Type>::GetNode(value,p->link);
if(p->link==NULL) last=newnode;
p->link=newnode;
return 1;
}
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;
}
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;
}
#include <iostream.h>//测试程序
#include "List.h"
void main(void)
{List<int> list0(0);
for(int i=0;i<10;i++)
list0.Insert(i,i);
}
出错信息是list类中的Find不能重载,还有在调用GetNode创建一个新节点时,不能调用GetNode。请大家帮帮忙!