储存int类型的集合类

编程实现一个存储任意类型int的集合类(即模板类),要求如下:

(1) 集合中的元素用动态链表存放

(2) 提供必要的构造函数、移动构造函数、赋值运算符重载函数等

(3) 能显示集合中元素个数

(4) 能显示集合中的元素

(5) 能向集合中增加元素(不能出现重复)

(6) 能从集合中删除元素

(7) 能实现两个集合的并运算(+)

(8) 能实现两个集合的交运算(*)

(9) 能实现两个集合的差集(-)

(10) 能使用<<运算符对集合类对象进行输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<iostream>
using namespace std;

class Node{
public:
Node* _next;
int value;
};


class Set{
private:
Node* head;
Node* tail;
// int _size;

public:
Set();
Set(Set && a);
void operator =(Set & a);
void operator =(Set && a);
int display_s();
void show();
void add(int a);
void del(int a);
Set operator +(Set &b);
Set operator *(Set &b);
Set operator -(Set &b);
friend ostream& operator << (ostream& out,Set& a);
};


Set::Set()
{
// _size=0;
head=NULL;
tail=NULL;
}


Set::Set(Set && a)
{
head=a.head;
tail=a.tail;
// _size=a._size;
a.head=a.tail=nullptr;
}



void Set::operator =(Set & a)
{
head=a.head;
tail=a.tail;
}

void Set::operator =(Set && a)
{
head=a.head;
tail=a.tail;
a.head=a.tail=nullptr;
}


int Set::display_s()
{
int num=0;
for(Node* i=head;i!=NULL;i=i->_next)
{
num++;
}
// cout<<num<<endl;
return num;
}


void Set::show()
{
for(Node* i=head;i!=NULL;i=i->_next)
{
cout<<i->value<<" ";
}
cout<<endl;
}


void Set::add(int a)
{
for(Node* i=head;i!=NULL;i=i->_next)
if(i->value==a) return;

if(head==NULL)
{
head=tail=new Node;
head->value=a;
head->_next=NULL;
return;
}

tail->_next=new Node;
tail=tail->_next;
tail->value=a;
tail->_next=NULL;
}


void Set::del(int a)
{
if(head==NULL) return;
if(head->value==a)
{
Node* cur=head;
head=head->_next;
delete cur;
return;
}
for(Node* i=head;i!=tail;i=i->_next)
{
Node* p=i->_next;
if(p->value==a)
{
i->_next=p->_next;
delete p;
}
}
}



Set Set::operator +(Set &b)
{
Set kkk;
for(Node* i=head;i!=NULL;i=i->_next)
{
kkk.add(i->value);
}
for(Node* i=b.head;i!=NULL;i=i->_next)
{
kkk.add(i->value);
}
return kkk;
}



Set Set::operator -(Set &b)
{
Set kkk;
for(Node* i=head;i!=NULL;i=i->_next)
{
kkk.add(i->value);
}
for(Node* i=b.head;i!=NULL;i=i->_next)
{
kkk.del(i->value);
}
return kkk;
}


Set Set::operator *(Set &b)
{
Set kkk;
for(Node* i=head;i!=NULL;i=i->_next)
{
for(Node* j=b.head;j!=NULL;j=j->_next)
{
if(i->value==j->value)
kkk.add(i->value);
}
}

return kkk;
}


ostream& operator << (ostream& out,Set &a)
{
out<<"size:"<<a.display_s()<<endl;
for(Node* i=a.head;i!=NULL;i=i->_next)
{
out<<i->value<<" ";
}
return out;
}

int main()
{
Set kkk;
kkk.add(1321);
kkk.add(124);
kkk.show();

Set kkk1;
kkk1.add(1321);
kkk1.add(24);

kkk1=kkk+kkk;
cout<<(kkk1);

return 0;
}