类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
#include <iostream>
using namespace std;
template <class T>
class list;
template <class T>
class listdate{
friend class list<T>;
public:
T date; //节点里面数据
listdate<T>* us; //指向下一个节点指针
};
template <class T>
class list{
public:
listdate<T>* first; //链表头指针
listdate<T>* lost; //末尾指针
list(){ first = lost = 0;}
~list(){
listdate<T>* dec = first;
while(first)
{
dec = first->us;
delete first;
first = dec;
}
}
list& sernt(T x) //链表后面 插入数据
{
listdate<T>* c = new listdate<T>;
c->date = x;
c->us = 0;
if(!first)
{
lost = first = c;
}
else
{
lost->us = c;
lost = c;
}
return *this;
}
friend ostream& operator<<(ostream& out,const list<T>& x)
{
listdate<T>* a = x.first;
while(a)
{
out << a->date << endl;
a = a->us;
}
return out;
}
int number() //计算表的元素数目
{
int a = 0;
listdate<T>* c = first;
while(c)
{
a++;
c = c->us;
}
return a;
}
list(const list<T>& a)//不知道这里那有问题!
{
listdate<T>* box = new listdate<T>;
listdate<T>* c = a.first;
first = lost = box;
int i = 0;
while(c)
{
i++;
lost->date = c->date;
if (i == (a.number() - 1))//类中的复制构造函数的参数不能调用类中的公有方法吗? 这里不能用number() ,通不过呀!
{
lost->us = 0;
break;
}
box = new listdate<T>;
lost->us = box;
lost = box;
c = c->us;
}
}
list<T>& operator=(list<T> a)//不知道这里那有问题!
{
if(this == &a)
return *this;
listdate<T>* box = new listdate<T>;
listdate<T>* c = a.first;
first = lost = box;
int i = 0;
while(c)
{
i++;
lost->date = c->date;
if (i == (a.number() - 1))
{
lost->us = 0;
break;
}
box = new listdate<T>;
lost->us = box;
lost = box;
c = c->us;
}
return *this;
}
};
template <class T>
list<T> toC(list<T>& A,list<T>& B);
int main()
{
list<int> a,b;
a.sernt(10);
a.sernt(20);
a.sernt(30);
b.sernt(10);
b.sernt(10);
list<int> c ;
c = toC(a,b);
list<int> d = c;
cout << c.number() << endl;
cout << c << endl;
cout << d << endl;
system("pause");
return 0;
}
template <class T>
list<T> toC(list<T>& A,list<T>& B) //两个表合并成一个表并返回
{
list<T> c;
c.first = A.first;
A.lost->us = B.first;
c.lost = B.lost;
return c;
}
高手指点
网友回答:
哦
gx
^_^ 捡分啰!
hehe
gx