以有序单链表表示集合,能够完成集合的并、交、差等运算。要求:1)集合元素限定为英文字母;2)输出结果以字符序列形式输出?

以有序单链表表示集合,能够完成集合的并、交、差等运算。要求:1)集合元素限定为英文字母;2)输出结果以字符序列形式输出。

帮你实现了框架和交集,剩下的交给你了,编程还是要增强动手能力呀

#include <vector>
#include <string>
#include <numeric>
#include <iostream>

using namespace std;

typedef struct sortedSingleNode {
    sortedSingleNode* next;
    char data;
}node;

//交集
int node_intersect(node* n1, node* n2, string* s) {
    node* t1 = n1;
    node* t2 = n2;
    char t = 0;
    vector<char> n;

    if (n1 == NULL || n2 == NULL || s == NULL)
        return 0;

    while (t1 != NULL && t2 != NULL) {
        if (t1->data > t2->data) {
            t1 = t1->next;
        } else if (t1->data < t2->data) {
            t2 = t2->next;
        } else {
            if (t == t1->data) {
                t1 = t1->next;
                t2 = t2->next;
                continue;
            } else {
                t = t1->data;
                n.push_back(t);
            }
        }
    }

    *s = accumulate(n.begin(), n.end(), *s);
    return 1;
}

//并集
int node_union(node* n1, node* n2, string* s) {
    return 1;
}


//差集
int node_diff(node* n1, node* n2, string* s) {
    return 1;
}

int node_add(node* n, char c) {
    node* nn = new node();
    nn->data = c;
    nn->next = NULL;
    node* t = n;
    while (t->next != NULL) {
        t = t->next; 
    }
    t->next = nn;
}

int main() {
    node n1;
    node n2;

    string s;

    n1.data = 'z';
    n1.next = NULL;
    node_add(&n1, 't');
    node_add(&n1, 'r');
    node_add(&n1, 'b');
    node_add(&n1, 'a');

    n2.data = 't';
    n2.next = NULL;
    node_add(&n2, 'r');
    node_add(&n2, 'c');
    node_add(&n2, 'b');
    node_add(&n2, 'a');

    node_intersect(&n1, &n2, &s);

    cout << s << endl;

    return 0;
}