asmallpig1 2019-12-24 22:43 采纳率: 0%
浏览 879

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

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

  • 写回答

1条回答 默认 最新

  • hust_jixing 2019-12-25 14:25
    关注

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

    #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;
    }
    
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?