
使用顺序表计算交集
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本文章介绍了如何利用顺序表这一数据结构来高效地计算两个集合的交集,并提供了相应的算法步骤和示例代码。
```cpp
int main() {
list a;
list b;
list c;
int x = 100, y = 100, i = 1, j = 1;
int k = 1;
cout << 请输入A集合中的数,以数字0结束: << endl;
while (true) {
cin >> x;
if (x == 0)
break;
a.insert(i, x);
i++;
cout << x << ;
}
cout << endl;
cout << 请输入B集合中的数,以数字0结束: << endl;
while (true) {
cin >> y;
if (y == 0)
break;
b.insert(j, y);
j++;
cout << y << ;
}
cout << endl;
i = 1; j = 1;
while (i <= a.length() && j <= b.length()) {
a.get_element(i, x);
b.get_element(j, y);
if (x > y)
j++;
else if (x == y) {
c.insert(k, x);
i++;
j++;
k++;
} else {
c.insert(k, x);
k++;
i++;
}
}
k = 1;
cout << A交B={;
while (k <= c.length()) {
c.get_element(k, x);
k++;
cout << x;
}
cout << } << endl;
return 0;
}
```
全部评论 (0)


