template <class T>class link{
public:
link<T> *insert(T val);
private:
link(T linkValue,link<T> *nextPtr);
link(const link<T> &source);
link<T> *duplicate()const;
T value;
link<T> *ptrToNextLink;
friend class list<T>;
friend class listIterator<T>;
};
template<class T> link<T>* link<T>::insert(T val){
ptrToNextLink = new link<T>(val,ptrToNextLink);
return ptrToNextLink;
}
template<class T> link<T>::link(T val,link<T> *nxt)
:value(val),ptrToNextLink(nxt){
}
template<class T> link<T>::link(const link<T> &source)
:value(source.value),ptrToNextLink(source.ptrToNextLink){
}
template<class T> link<T> *link<T> ::duplicate()const{
link<T>* newlink;
if (ptrToNextLink != 0)
newlink = new link<T>(value,ptrToNextLink->duplicate());
else
newlink = new link<T>(value,0);
return newlink;
}
/*----------------------------------------------------*/
template <class T> class list{
public:
list();
list(const list<T>& source);
virtual ~list();
virtual void add(T value);
virtual void deleteAllValues();
T firstElement() const;
virtual int includes(T value) const;
int isEmpty() const;
virtual void removeFirst();
list<T> *duplicate()const;
private:
link <T> *ptrToFirstLink;
friend class listIterator<T>;
};
template <class T> list<T>::list()
:ptrToFirstLink(0){
}
template <class T> list<T>::list(const list<T> &source){
if (source.isEmpty())
ptrToFirstLink = 0;
else{
link<T> *firstLink = source.ptrToFirstLink;
ptrToFirstLink = firstLink->duplicate();
}
}
template <class T> list<T>::~list(){
deleteAllValues();
}
template <class T> void list<T>::add(T val){
ptrToFirstLink=new link<T>(val,ptrToFirstLink);
}
template <class T> void list<T>::deleteAllValues(){
link<T>* next;
for(link<T> *p=prtToFirstLink;p!=0; p=next){
next=p->ptrToNextLink;
p->ptrToNextLink = 0;
delete p;
}
prtToFirstLink = 0;
}
template <class T> T list<T>::firstElement()const{
return ptrToFirstLink->value;
}
template <class T> int list<T>::includes(T v)const{
for (link<T> *p=prtToFirstLink;p;p=p->ptrToNextLink)
if (v == p->value) return 1;
return 0;
}
template <class T> int list<T>::isEmpty() const{
return ptrToFirstLink==0;
}
template <class T> void list<T>::removeFirst(){
link<T> *p = ptrToFirstLink;
ptrToFirstLink = p->ptrToNextLink;
delete p;
}
template <class T> list<T> *list<T>::duplicate() const{
list<T> *newlist = new list<T>;
if (ptrToFirstLink)
newlist->ptrToFirstLink = ptrToFirstLink->duplicate();
return newlist;
}
/*----------------------------------------------------*/
template<class T>class listIterator:public iterator<T>{
public:
listIterator(list<T>& aList);
virtual int init();
virtual T operator()();
virtual int operator!();
virtual int operator++();
virtual void operator=(T value);
void removeCurrent();
void addBefore(T newValue);
void addAfter(T newValue);
protected:
link<T>* currentLink;
link<T>* previousLink;
list<T>& theList;
};
template<class T> listIterator(list<T>& aList)
:theList(aList){
init();
}
template<class T> init listIterator<T>::init(){
previousLink==0;
currentLink=theList.ptrToFirstLink;
return currentLink!=0;
}
template<class T> listIterator::operator()(){
return currentLink->value;
}
template<class T>int listIterator<T>::opeartor!(){
if(currentLink!=0)
return 1;
if(previousLink!=0)
return previousLink->ptrToNextLink!=0;
return theList.ptrToFirstLink!=0;
}
template<class T>int listIterator<T>::operator++(){
if(currentLink==0){
if(previousLink==0)
currentLink=thrList.ptrToFirstLink;
else
currentLink=previousLink->ptrToNextLink;
}
else{
previousLink=currentLink;
currentLink=currentLink->ptrToNextLink;
}
return currentLink!=0;
}
template<class T>void listIterator<T>::operator=(T val){
currentLink->value=val;
}
template<class T>void listIterator<T>::removecurrent(){
if(previousLink==0)
theList.ptrToFirstLink=currentLink->ptrToNextLink;
else
previousLink->ptrToNextLink=currentLink->ptrToNextLink;
delete currentLink;
currentLink=0;
}
template<class T>void listIterator<T>::addBefore(T val){
if(previousLink)
previousLink=previousLink->insert(val);
else{
theList.List<T>::add(val);
previousLink=theLink.ptrToFirstLink;
}
}
template<class T> void listIterator<T>::addAfter(T val){
if(currentLink!=0)
currentLink->insert(val);
else if(previousLink!=0)
previousLink->insert(val);
else theList.List<T>::add(val);
}
/*----------------------------------------------*/
template <class T>class iterator{
public:
virtual int init()=0;
virtual int operator !()=0;
virtual T operator()()=0;
virtual int operator ++()=0;
virtual void operator=(T newValue)=0;
};
/*----------------------------------------------------*/
以上是我编写的几个class,但我不太会将这几个class整和起来,形成一个完整的链表类
我用的是VC,也用了模板,最后的类应该可以遍历整个链表