📄 list.h
字号:
//#ifndef list
//#define list
//#include"link.h"
#include<assert.h>
template<class T>class Link;
template<class T>class listIterator;
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;
protected:
Link<T> *ptrtofirstLink;
friend class listIterator<T>;
};
template<class T>
List<T>::isEmpty() const{
return ptrtofirstLink==0;
}
template<class T>
void List<T>::add(T val){
ptrtofirstLink=new Link<T>(val,ptrtofirstLink);
assert(ptrtofirstLink!=0);
}
template<class T>
T List<T>::firstElement() const{
assert(ptrtofirstLink!=0)
return ptrtofirstLink->value;
}
template<class T>
void List<T>::removeFirst(){
assert(ptrtofirstLink!=0);
Link<T> *p=ptrtofirstLink;
ptrtofirstLink=p->ptrtonextLink;
delete p;
}
template<class T>
int List<T>::includes(T v) const{
for(Link<T>*p=ptrtofirstLink;p;p->ptrtonextLink)
if(v==p->value)
return 1;
return 0;
}
template<class T>
void List<T>::deleteAllValues(){
Link<T>*next;
for(Link<T>*p=ptrtofirstLink;p!=0;p=next){
next=p->ptrtonextLink;
p->ptrtonextLink=0;
delete p;
}
ptrtofirstLink=0;
}
template<class T>
List<T>*List<T>::duplicate() const{
List<T>*newlist=new List<T>;
assert(newlist!=0);
if(ptrtofirstLink)
newlist->ptrtofirstLink=ptrtofirstLink->duplicate();
return newlist;
}
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>
List<T>::List():ptrtofirstLink(0){
}
//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -