65,187
社区成员




#include<iostream>
using namespace std;
template<typename T>
struct Node
{
T data;
Node<T> *next;
};
template<class T>
class Set
{
friend ostream& operator<< <>(ostream& out, const Set<T>& s);
public:
Set()
{
head = new Node<T>;
head->next = NULL;
}
Set(const T& s)
{
Node<T> *p, *pnew, *ptail;
p = s.head->next;
ptail = head;
while (p)
{
pnew = new Node<T>;
ptail->next = pnew;
pnew->date = p->data;
p = p->next;
ptail = pnew;
}
ptail->next = NULL;
}
~Set()
{
Node<T>* p;
while (head!=NULL)
{
p = head->next;
delete head;
head = p;
}
}
void print();
void in();
private:
Node<T> *head;
};
template<typename T>
void Set<T>::in()
{
int i, n;
Node<T> *p, *pnew;
p = head;
cout << "input number" << endl;
cin >> n;
for (i = 0; i < n; i++)
{
pnew = new Node<T>;
cin >> pnew->data;
p->next = pnew;
p = pnew;
}
p->next = NULL;
}
template<typename T>
ostream& operator<<(ostream& out, const Set<T>& s)
{
Node<T> *p;
p = s.head->next;
while (p)
{
cout << p->data << ' ';
p = p->next;
}
return out;
}
void main()
{
Set<int> s;
s.in();
cout << s<<endl;
Set<int> a(s);
cout << a << endl;
}