要求:使用未排序的单链表,使用类模板,运算符重载,链式存储,自定义集合类型
分析:1.首先新建一个链表,并将其作为返回结果
2.二重循环遍历,利用结点指针依次后移比较链表的元素,对于交集,将既在A又在B中的元素加入新建的链表,对于并集,将A和B所有元素加入链表,对于差集,将在A或B中的元素加入新建链表。
IDE:Visual Studio 2022
首先自定义链表类,在常用的函数上增加运算符重载的功能
自定义链表类 linkList.h
#pragma once
#include<iostream>
using namespace std;
template<typename T>
class linkList
{
public:
struct node
{
T data;
node* next;
};
linkList() { count = 0; head = new node; head->next = NULL; }
linkList operator+(const linkList& x);
linkList operator-(const linkList& x);
linkList operator*(const linkList& x);
linkList& operator=(const linkList& x);
~linkList(){
while (!empty())
{
del(count);
}
delete head;
}
int length()const;
void get(const int i, T& x);
node* locate(const T x)const {
if (empty()) {
cout << "链表是空的" << endl;
}
node* p = head->next;
while (p!=NULL){
if (p->data == x) {
return p;
}
else {
p = p->next;
}
}
cout << "数据不存在" << endl;
return NULL;
}
void insert(const int i, const T x);
void del(const int i);
void print();
void sort();
bool empty();
node* top() { return head;}
private:
int count;
node* head;
};
自定义链表类 linkList.cpp
#include "linkList.h"
template<typename T>
int linkList<T>::length()const {
return count;
}
template<typename T>
linkList<T> linkList<T>::operator+(const linkList& x) //并集
{
linkList<T> C;
int k = 1;
for (node* i = x.head->next; i != NULL; i=i->next)
{
C.insert(k++, i->data);
}
node* p1 = this->head->next;
node* p2 = x.head->next;
while (p1 != NULL)
{
int flag = 1;
p2 = x.head->next;
while (p2 != NULL)
{
if (p1->data == p2->data)
{
flag = 0;
break;
}
p2 = p2->next;
}
if (flag==1)
{
C.insert(k++, p1->data);
}
p1 = p1->next;
}
return C;
}
template<typename T>
linkList<T> linkList<T>::operator-(const linkList& x) //差集
{
linkList<T> C;
int k = 1;
node* p1 = this->head->next;
node* p2 = x.head->next;
while (p1 != NULL)
{
int flag = 1;
p2 = x.head->next;
while (p2 != NULL)
{
if (p1->data == p2->data)
{
flag = 0;
break;
}
p2 = p2->next;
}
if (flag == 1)
{
C.insert(k++, p1->data);
}
p1 = p1->next;
}
node* p3 = this->head->next;
node* p4 = x.head->next;
while (p4 != NULL)
{
int flag = 1;
p3 = this->head->next;
while (p3 != NULL)
{
if (p4->data == p3->data)
{
flag = 0;
break;
}
p3 = p3->next;
}
if (flag == 1)
{
C.insert(k++, p4->data);
}
p4 = p4->next;
}
return C;
}
template<typename T>
linkList<T> linkList<T>::operator*(const linkList<T>& x) //交集
{
linkList<T> C;
node* p1 = this->head->next;
node* p2 = x.head->next;
int k = 1;
while (p2 != NULL)
{
p1 = this->head->next;
while (p1 != NULL)
{
if (p2->data == p1->data)
{
C.insert(k++, p2->data);
break;
}
p1 = p1->next;
}
p2 = p2->next;
}
return C;
}
template<typename T>
linkList<T>& linkList<T>::operator=(const linkList& x)
{
if (head!=NULL){
node* p0 = head->next;
delete head;
node* q0 = NULL;
while (p0 != NULL)
{
q0 = p0->next;
delete p0;
p0 = q0;
}
}
this->count = x.length();
if (x.head == NULL) { head = NULL; }
else
{
head = new node();
node* p1 = head;
node* p2 = x.head->next;
for (int i = 0; i < length(); i++)
{
node* temp = new node();
temp->data = p2->data;
p1->next = temp;
p1 = p1->next;
p2 = p2->next;
}
}
return *this;
}
template<typename T>
void linkList<T>::get(const int i, T& x) {
if (empty()){
cout << "链表是空的" << endl;
}
node* p = head->next;
int j = 1;
while (p!=NULL&&j!=i){
p = p->next;
j++;
}
if (p==NULL){
cout << "数据不存在" << endl;
}
else {
x = p->data;
}
}
template<typename T>
void linkList<T>::insert(const int i, const T x) {
node* p = head;
if (i<1||i>count+1){
cout << "插入失败" << endl;
}
for (int e = 1; e < i; e++) {
p = p->next;
}
node* temp = new node;
temp->data = x;
if (temp!=NULL)
{
temp->next = p->next;
p->next = temp;
count++;
}
}
template<typename T>
void linkList<T>::del(const int i) {
node* p = head;
int j = 0;
if (i<1 || i>count + 1) {
cout << "删除失败" << endl;
}
else if (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
node* u = new node;
u = p->next;
if (u!=NULL)
{
p->next = u->next;
delete u;
count--;
}
}
template<typename T>
void linkList<T>::print() {
if (empty()){
cout << "链表是空的,打印无效" << endl;
}
else{
node* p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
}
template<typename T>
void linkList<T>::sort() {
if (empty()) {
cout << "链表是空的,排序无效" << endl;
}
else {
node* p = head->next;
node* q = p->next;
node* temp = head;
for (int i = 0; i < count-1; i++){
for (int j = i + 1; j < count; j++) {
if ((p->data)>(q->data))
{
p->next = q->next;
q->next = p;
temp->next = q;
}
temp = temp->next;
p = temp->next;
q = p->next;
}
}
}
}
template<typename T>
bool linkList<T>::empty() {
return count == 0;
}
输入测试数据来验证算法结果:
A:1 2 3 4 5
B:1 2 3 6 8
主函数 main.cpp
#include<iostream>
#include"linkList.cpp"
using namespace std;
int main()
{
linkList<int> A;
linkList<int> B;
linkList<int> C;
cout << "请输入5个集合A的元素:" << endl;
for (int i = 1; i <= 5; i++)
{
int ta;
cin >> ta;
A.insert(i, ta);
}
cout << "请输入5个集合B的元素:" << endl;
for (int i = 1; i <= 5; i++)
{
int tb;
cin >> tb;
B.insert(i, tb);
}
cout << endl;
C = A + B;
cout << "C = A + B 并集为:"; C.print();
cout << endl;
C = A * B;
cout << "C = A * B 交集为:";
C.print();
cout << endl;
C = A - B;
cout << "C = A - B 差集为:"; C.print();
cout << endl;
A = A - B;
cout << "A = A - B 差集为:"; A.print();
cout << endl;
return 0;
}
运行结果如下:
大一萌新努力学习ing,一点算法总结,仅供学习参考,如有不足欢迎指正!