#include
#include
using namespace std;
template
struct Node
{
Node(const T& d):c(d),next(0),pref(0){} // 这里加了const
T c;
Node *next, *pref;
};
template
class List
{
Node
public:
List();
void add(const T& c); // 这里加了const
void remove(const T& c); // 这里加了const
Node
void print();
~List();
};
template
List
template
void List
{
Node
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}
template
void List
{
if(!(Node
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}
template
Node
{
for(Node
if(p->c==n) return p;
return 0;
}
template
List
{
for(Node
first = first->next;
}
template
void List
{
for(Node
cout<
cout<<"\n";
}
int main()
{
List
dList.add(3.6); //因为这里传的是常量,所以形参要是const,以防止在函数中对常量作修改,同理其他函数
dList.add(5.8);
dList.print();
List
iList.add(5);
iList.add(8);
iList.print();
getch();
return 0;
}
#include
#include
using namespace std;
template
struct Node
{
Node(T& d):c(d),next(0),pref(0){}
T c;
Node *next, *pref;
};
template
class List
{
Node
public:
List();
void add(T& c);
void remove(T& c);
Node
void print();
~List();
};
template
List
template
void List
{
Node
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}
template
void List
{
if(!(Node
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}
template
Node
{
for(Node
if(p->c==n) return p;
return 0;
}
template
List
{
for(Node
first = first->next;
}
template
void List
{
for(Node
cout<
cout<<"\n";
}
int main()
{
List
//dList.add(3.6); //错误C2664: “List
//dList.add(5.8); //错误C2664: “List
double a=3.6,b=5.8;
dList.add(a);
dList.add(b);
dList.print();
List
//iList.add(5); //错误 C2664: “List
//iList.add(8); //错误 C2664: “List
int c=5,d=8;
iList.add(c);
iList.add(d);
iList.print();
getch();
return 0;/////////////////////应该有返回值
}
引用是不能传常量的。
int main()
{
List
double a = 3.6, b = 5.8;
int c = 5, d = 8;
dList.add(a);
dList.add(b);
dList.print();
List
iList.add(c);
iList.add(d);
iList.print();
getch();
}
#include"stdafx.h"
#include
#include
using namespace std;
template
struct Node
{
Node(T& d):c(d),next(0),pref(0){}
T c;
Node *next, *pref;
};
template
class List
{
Node
public:
List();
void add(T c);
void remove(T& c);
Node
void print();
~List();
};
template
List
template
void List
{
Node
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}
template
void List
{
if(!(Node
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}
template
Node
{
for(Node
if(p->c==n) return p;
return 0;
}
template
List
{
for(Node
first = first->next;
}
template
void List
{
for(Node
cout<
cout<<"\n";
}
int main()
{
List
dList.add(3.6);
dList.add(5.8);
dList.print();
List
iList.add(5);
iList.add(8);
iList.print();
getch();
}