//linklist.h
#include<iostream.h>
#include<stdlib.h>
template<class type>class linklist;
template<class type>class node
{
friend class linklist<type>;
private:
node<type>*next;
public:
type data;
node(node<type>*pnext=NUll);//构造函数,用于构造头结点
node(const type&item,node<type>*pnext=NULL);//构造函数,用于构造非头结点
void setnext(node<type>*p){next=p;}
void setdata(const type&x){data=x;}
~node();
};
template<class type>class linklist
{
private:
node<type>*head;
node<type>*pcurrent;
public:
linklist();
~linklist();
int length()const;
type getCurrent()const;
node<type>*locate(type&x);
void insterBefore(const type&x);
void insterAfter(const type&x);
type deleteCurrent();
int isempty()const;
void clear();
node<type>*reset(int i);//使第i个结点成为当前结点
node<type>*next();//使当前结点指针指向下一个节点
int endofList()const;
void freeList();
};
//linklist.cpp
#include"linklist.h"
template<class type>node<type>::node(node<type>*pnext)
{
next=pnext;
}
template<class type>node<type>::node(const type&item,node<type>*pnext)
{
data=item;
next=pnext;
}
template<class type>linklist<type>::linklist()
{
head=pcurrent=new node<type>();
head->next=NULL;
}
template<class type>linklist<type>::~linklist()
{
}
template<class type>void linklist<type>::freeList()
{
clear();
delete head;
}
template<class type>int linklist<type>::length()const
{
node<type>*p=head->next;
int len=0;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
template<class type>type linklist<type>::getCurrent()const
{
if (pcurrent==head||pcurrent==NULL)
{
cerr<<"未取到数据值"<<endl;
exit(1);
}
return pcurrent->data;
}
template<class type>node<type>*linklist<type>::locate(type&x)
{
pcurren=head->next;
while(pcurrent!=NULL)
{
if(pcurrent->data==x)
break;
else
pcurrent=pcurrent->next;
}
return pcurrent;
}
template<class type>void linklist<type>::insterBefore(const type&x)
{
if(head->next==NULL)
{
node<type>*newnode=new node<type>(x,NULL);
head->next=pcurrent=newnode;
}
else
{
node<type>*newnode=new node<type>(x,pcurrent);
node<type>*p=head;
while(p->next!=pcurrent)p=p->next;
p->next=newnode;
pcurrent=newnode;
}
}
template<class type>void linklist<type>::insterAfter(const type&x)
{
if(head->next==NULL||pcurrent==NULL)
{
node<type>*newnode=new node<type>(x,head->next);
head->next=pcurrent=newnode;
}
else
{
node<type>*newnode=new node<type>(x,pcurrent->next);
pcurrent->next=newnode;
pcurrent=newnode;
}
}
template<class type>type linklist<type>::deleteCurrent()
{
if(pcurrent==head||pcurrent==NULL)
{
cerr<<"不可删除"<<endl;
exit(1);
}
node<type>*p=head;
while(p->next!=pcurrent)p=p->next;
p->next=pcurrent->next;
type data1=pcurrent->data;
delete pcurrent;
pcurrent=p->next;
return data1;
}
template<class type> int linklist<type>::isempty()const
{
if(head->next==NULL)return 1;
else return 0;
}
template<class type>void linklist<type>::clear()
{
node<type>*p,*q;
p=head->next;
while(p!=NULL)
{
q=p;
p=p-nxet;
delete q;
}
pcurrent=head;
}
template<class type>node<type>*linklist<type>::reset(int i)
{
if(head==NULL||i<0)return NULL;
if(i==0)
{
pcurrent=head;
return head;
}
node<type>*p=head;
int k=0;
while(p!=NULL&&k<i)
{
p=p->next;
k++;
}
pcurrent=p;
return p;
}
template<class type>node<type>*linklist<type>::next()
{
if(pcurrent!=NULL)
pcurrent=pcurrent->next;
return pcurrent;
}
template<class type>int linklist<type>::endofList()const
{
if(pcurrent==NULL)
return 1;
else
return 0;
}
//main.cpp
#include"linklist.h"
int sum(const linklist<int>list)
{
if(list.isempty()==1)return 0;
int item=list.reset(1)->data;
node<int>*p=list.reset(1);
while(list.endofList())
{
p=list.next();
if(p)
item+=p->data;
}
return item;
}
void main()
{
linklist<int>list2;
cout<<"空链表的和为"<<endl;
cout<<sum(list2)<<endl;
for(int i=0;i<5;i++)
list2.insterAfter(i*i+100);
cout<<"链表中的数据元素分别是:"<<endl;
node<int>*p=list2.reset(1);
while(!list2.endofList())
{
cout<<p->data<<" ";
p=list2.next();
}
cout<<"链表中的各个元素和为:"<<endl;
cout<<sum(list2)<<endl;
p=list2.reset(1);
cout<<"依次所删除的接点的值分别为";
while(!list2.endofList())
cout<<" "<<list2.deleteCurrent()<<endl;
list2.freeList();
}
--------------------Configuration: 单链表 - Win32 Debug--------------------
Compiling...
liklist.cpp
main.cpp
E:\单链表\main.cpp(5) : error C2662: 'reset' : cannot convert 'this' pointer from 'const class linklist<int>' to 'class linklist<int> &'
Conversion loses qualifiers
E:\单链表\main.cpp(5) : error C2227: left of '->data' must point to class/struct/union
E:\单链表\main.cpp(6) : error C2662: 'reset' : cannot convert 'this' pointer from 'const class linklist<int>' to 'class linklist<int> &'
Conversion loses qualifiers
E:\单链表\main.cpp(9) : error C2662: 'next' : cannot convert 'this' pointer from 'const class linklist<int>' to 'class linklist<int> &'
Conversion loses qualifiers
linklist.cpp
Error executing cl.exe.
单链表.exe - 4 error(s), 0 warning(s)
这个几个错误改不了.............