//=====================================
// f1407.cpp
// Using Class Template Specialization
//=====================================
#include<iostream>
using namespace std;
//-------------------------------------
template<typename T>
struct Node{
Node(const T& d):c(d),next(0),pref(0){}
T c;
Node *next, *pref;
};//-----------------------------------
// 以下为基本类模板
template<typename T>
class List{
Node<T> *first, *last;
public:
List();
void add(const T& c);
void remove(const T& c);
Node<T>* find(const T& c)const;
void print()const;
~List();
};//-----------------------------------
template<typename T>
List<T>::List():first(0),last(0){}
//-------------------------------------
template<typename T>
void List<T>::add(const T& n){
Node<T>* p = new Node<T>(n);
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}//------------------------------------
template<typename T>
void List<T>::remove(const T& n){
Node<T>* p = find(n);
if(!p) return;
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}//------------------------------------
template<typename T>
Node<T>* List<T>::find(const T& n)const{
for(Node<T>* p=first; p; p=p->next)
if(p->c==n) return p;
return 0;
}//------------------------------------
template<typename T>
List<T>::~List(){
for(Node<T>* p; p=first; delete p)
first = first->next;
}//------------------------------------
template<typename T>
void List<T>::print()const{
for(Node<T>* p=first; p; p=p->next)
cout<<p->c<<" ";
cout<<"\n";
}//------------------------------------
class Cat{
string name;
public:
Cat(const string& n):name(n){}
bool operator==(const Cat& c){ return name == c.name; }
friend ostream& operator<<(ostream& o, const Cat& c){ return o<<c.name; }
};//-----------------------------------
// 以下为模板定做
template<>
class List<Cat>{
Node<Cat> *first, *last;
public:
List();
void add(const Cat& c);
void remove(const Cat& c);
Node<Cat>* find(const Cat& c)const;
void print()const;
~List();
};//-----------------------------------
List<Cat>::List():first(0),last(0){}
//-------------------------------------
void List<Cat>::add(const Cat& n){ // 与类模板的成员函数实现上有差异
if(find(n)) return;
Node<Cat>* p = new Node<Cat>(n);
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}//------------------------------------
void List<Cat>::remove(const Cat& n){
Node<Cat>* p = find(n);
if(!p) return;
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}//------------------------------------
Node<Cat>* List<Cat>::find(const Cat& n)const{
for(Node<Cat>* p=first; p; p=p->next)
if(p->c==n) return p;
return 0;
}//------------------------------------
List<Cat>::~List(){
for(Node<Cat>* p; p=first; delete p)
first = first->next;
}//------------------------------------
void List<Cat>::print()const{
for(Node<Cat>* p=first; p; p=p->next)
cout<<p->c<<" ";
cout<<"\n";
}//------------------------------------
int main(){
List<Cat> cList;
cList.add(string("alice"));
cList.add(string("luise"));
cList.add(string("luise"));
cList.print();
List<int> iList;
iList.add(5);
iList.add(8);
iList.add(8);
iList.print();
}//====================================
钱能C++书中代码,课件
需积分: 3 163 浏览量
2008-08-26
12:39:09
上传
评论
收藏 414KB RAR 举报
guiji1988
- 粉丝: 1
- 资源: 11
最新资源
- 基于python实现的三次样条插值和均值插值法实现
- Python语言教程2-python批量图片大小处理-多文件夹
- Python语言教程1-python批量图片重命名,将后缀某几个不想要的字去除
- Space Combat Kit 太空战斗套件Unity游戏开发插件资源unitypackage C#
- Universal Device Preview 通用设备预览Unity游戏开发插件资源unitypackage
- Paladin Anim Set 圣骑士动画集Unity游戏动作动画插件资源unitypackage
- 计算机财务管理期末考报表部分题目及答案.doc
- 计算机软件维护论文.doc
- 计算机软件著作权授权书.docx
- 计算机键盘教学案例.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0