钾钇 2021-07-07 10:23 采纳率: 100%
浏览 56
已结题

数据结构(队列实现数学上的集合)

只用C,不用C++和java,求具体能运行的代码
用一个队列实现数学上的集合(Set)的元素判定(属于、is a member of)、子集判定(包含于、is a subset of、is contained in)、交(intersection)、并(union)、差(complement、set-theoretic difference)运算,集合元素限定为整数。只能利用队列的入队和出队操作访问队列中元素。

  • 写回答

4条回答 默认 最新

  • bx112233 2021-07-07 17:08
    关注

    #include <stdio.h>
    #include <stdlib.h>
    //数据类型
    #define ElemType int

    typedef struct QueueNode
    {
    ElemType data; //数据域
    struct QueueNode *next; //指针域
    }QueueNode;

    typedef struct LinkQueue
    {
    QueueNode *head; //队头指针
    QueueNode *tail; //队尾指针
    }LinkQueue;

    typedef struct Node {
    int data;
    struct Node *next;
    }Node;

    //创建新节点函数
    Node *CreateNode(int value)
    {
    //为新节点申请空间
    Node new_node = (Node)malloc(sizeof(Node));
    //给新节点赋值
    new_node->data = value;
    //将新节点的next指向空
    new_node->next = NULL;
    return new_node;
    }

    void Emptyqueue(Node **head)//释放队列空间
    {
    Node *cur = *head;
    Node *temp;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    //遍历队列
    while (cur != NULL)
    {
        temp = cur->next;
        free(cur);
        //移动cur,以达到遍历队列的目的
        cur = temp;
    }
    
    *head = NULL;
    

    }

    //顺序打印队列元素
    void queuePrint(Node *head)
    {
    Node *cur;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    cur = head;
    //遍历队列
    while (cur != NULL)
    {
        //打印元素和其对应的地址
        printf("%d\n", cur->data);
        //移动cur,以达到遍历队列的目的
        cur = cur->next;
    }
    printf("\n");
    

    }

    //入队函数,插入链表尾部
    Node *queuePushBack(Node **head, int value)
    {
    Node *cur;
    Node *new_node;
    //非法输入
    if (head == NULL)
    {
    return NULL;
    }
    //空队列
    if (*head == NULL)
    {
    //直接创建一个新的节点完成元素插入
    *head = CreateNode(value);
    return NULL;
    }
    else
    {
    cur = *head;
    //遍历队列,让cur指向最后一个元素
    while (cur->next != NULL)
    {
    cur = cur->next;
    }
    //创建一个新节点
    new_node = CreateNode(value);
    //将最后一个元素的next指向新节点
    cur->next = new_node;
    return new_node;
    }
    }

    //出队函数,从链表头删除
    void queuePop(Node **head)
    {
    //非法输入
    if (head == NULL)
    {
    return;
    }
    //空队列没有可删除的元素
    if (*head == NULL)
    {
    return;
    }
    else
    {
    //创建一个新的指针指向第二个元素
    Node *new_node = (*head)->next;
    //将头结点的next指向空
    (*head)->next = NULL;
    //删除该头结点
    free(*head);
    //将第二个元素设置成新的队头
    *head = new_node;
    }
    return;
    }

    int IN_SET(Node *L, int value)//判断数据是否存在队列中
    {
    int ret = 1;
    Node *p1 = L;
    if (L == NULL) return ret;
    while (p1)
    {
    if (value == p1->data)
    {
    ret = 0;
    break;
    }
    p1 = p1->next;
    }

    return ret;
    

    }

    int INSERT_SET(Node **L, int value)//插入数据
    {
    int ret = 0;
    if (IN_SET(*L, value))//判断是否存在
    {
    queuePushBack(L, value);//插入
    ret = 1;
    }

    return ret;
    

    }

    int child(Node * L1, Node * L2)//判断集合1是否为集合2的子集,是返回1,不是返回0
    {
    Node * L, *p1, *p2;
    int find = 0;
    p1 = L1;
    p2 = L2;
    while (p1)//查找集合1
    {
    find = 0;
    p2 = L2;
    while (p2)//查找集合2中是否包含集合1的元素
    {
    if (p1->data == p2->data){//如果找到表示存在,find赋值为1并跳出
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) return 0;//一旦存在集合1中元素未包含在集合2,表示集合1非集合2子集,返回0停止判断
        p1 = p1->next;
    }
    
    return 1;//集合1全部包含在集合2中,集合1是集合2子集
    

    }

    Node* intersection(Node * L1, Node * L2)//交集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//查找集合1
    {
    p2 = L2;
    while (p2)//查找集合2
    {
    if (p1->data == p2->data){//集合1和集合2都存在的,插入到交集队列
    queuePushBack(&L, p1->data);
    break;
    }

            p2 = p2->next;
        }
        p1 = p1->next;
    }
    
    return L;
    

    }

    Node* Union(Node * L1, Node * L2)//并集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//集合1插入并集集合
    {
    queuePushBack(&L, p1->data);
    p1 = p1->next;
    }

    while (p2)//集合2插入并集集合
    {
        if (IN_SET(L, p2->data)) queuePushBack(&L, p2->data);//集合2插入时判断是否重复
        p2 = p2->next;
    }
    
    return L;
    

    }

    Node* difference(Node * L1, Node * L2)//差集
    {
    Node *L, *p1, *p2;
    int find;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//将集合1减集合2的结果存入差集合
    {
    find = 0;
    p2 = L2;
    while (p2)
    {
    if (p1->data == p2->data){//在集合2中查找集合1中元素是否存在
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) queuePushBack(&L, p1->data);//存在集合1不存在集合2的元素插入差集合
        p1 = p1->next;
    }
    
    return L;
    

    }

    int main()
    {
    FILE *fp = NULL; //文件句柄
    int data, t = 1, i = 0;
    Node *A = NULL, *B = NULL, *C = NULL, *D = NULL, *E = NULL;//队列
    int k = 1;//为0时推出创新
    while (k)
    {
    printf("1、创建队列AB\n");
    printf("2、判断子集关系\n");
    printf("3、求AB交集C\n");
    printf("4、求AB并集D\n");
    printf("5、求AB差集E\n");
    printf("0、退出\n");
    printf("请选择:");
    scanf_s("%d", &i);

        switch (i)
        {
        case 1:
            Emptyqueue(&A);
            Emptyqueue(&B);
            printf("开始创建A:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&A, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            t = 1;
            printf("开始创建B:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&B, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            break;
        case 2:
            if (child(A, B)) printf("集合A是集合B子集\n");
            else printf("集合A不是集合B子集\n");
    
            if (child(B, A)) printf("集合B是集合A子集\n");
            else printf("集合B不是集合A子集\n");
            break;
        case 3:
            Emptyqueue(&C);
            C = intersection(A, B);
            printf("集合AB的交集C:\n");
            queuePrint(C);
            break;
        case 4:
            Emptyqueue(&D);
            D = Union(A, B);
            printf("集合AB的并集D:\n");
            queuePrint(D);
            break;
        case 5:
            Emptyqueue(&E);
            E = difference(A, B);
            printf("集合AB的差E:\n");
            queuePrint(E);
            break;
        case 0:  k = 0; break;
        }
    }
    
    Emptyqueue(&A);
    Emptyqueue(&B);
    Emptyqueue(&C);
    Emptyqueue(&D);
    Emptyqueue(&E);
    return 0;
    

    }#include <stdio.h>
    #include <stdlib.h>
    //数据类型
    #define ElemType int

    typedef struct QueueNode
    {
    ElemType data; //数据域
    struct QueueNode *next; //指针域
    }QueueNode;

    typedef struct LinkQueue
    {
    QueueNode *head; //队头指针
    QueueNode *tail; //队尾指针
    }LinkQueue;

    typedef struct Node {
    int data;
    struct Node *next;
    }Node;

    //创建新节点函数
    Node *CreateNode(int value)
    {
    //为新节点申请空间
    Node new_node = (Node)malloc(sizeof(Node));
    //给新节点赋值
    new_node->data = value;
    //将新节点的next指向空
    new_node->next = NULL;
    return new_node;
    }

    void Emptyqueue(Node **head)//释放队列空间
    {
    Node *cur = *head;
    Node *temp;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    //遍历队列
    while (cur != NULL)
    {
        temp = cur->next;
        free(cur);
        //移动cur,以达到遍历队列的目的
        cur = temp;
    }
    
    *head = NULL;
    

    }

    //顺序打印队列元素
    void queuePrint(Node *head)
    {
    Node *cur;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    cur = head;
    //遍历队列
    while (cur != NULL)
    {
        //打印元素和其对应的地址
        printf("%d\n", cur->data);
        //移动cur,以达到遍历队列的目的
        cur = cur->next;
    }
    printf("\n");
    

    }

    //入队函数,插入链表尾部
    Node *queuePushBack(Node **head, int value)
    {
    Node *cur;
    Node *new_node;
    //非法输入
    if (head == NULL)
    {
    return NULL;
    }
    //空队列
    if (*head == NULL)
    {
    //直接创建一个新的节点完成元素插入
    *head = CreateNode(value);
    return NULL;
    }
    else
    {
    cur = *head;
    //遍历队列,让cur指向最后一个元素
    while (cur->next != NULL)
    {
    cur = cur->next;
    }
    //创建一个新节点
    new_node = CreateNode(value);
    //将最后一个元素的next指向新节点
    cur->next = new_node;
    return new_node;
    }
    }

    //出队函数,从链表头删除
    void queuePop(Node **head)
    {
    //非法输入
    if (head == NULL)
    {
    return;
    }
    //空队列没有可删除的元素
    if (*head == NULL)
    {
    return;
    }
    else
    {
    //创建一个新的指针指向第二个元素
    Node *new_node = (*head)->next;
    //将头结点的next指向空
    (*head)->next = NULL;
    //删除该头结点
    free(*head);
    //将第二个元素设置成新的队头
    *head = new_node;
    }
    return;
    }

    int IN_SET(Node *L, int value)//判断数据是否存在队列中
    {
    int ret = 1;
    Node *p1 = L;
    if (L == NULL) return ret;
    while (p1)
    {
    if (value == p1->data)
    {
    ret = 0;
    break;
    }
    p1 = p1->next;
    }

    return ret;
    

    }

    int INSERT_SET(Node **L, int value)//插入数据
    {
    int ret = 0;
    if (IN_SET(*L, value))//判断是否存在
    {
    queuePushBack(L, value);//插入
    ret = 1;
    }

    return ret;
    

    }

    int child(Node * L1, Node * L2)//判断集合1是否为集合2的子集,是返回1,不是返回0
    {
    Node * L, *p1, *p2;
    int find = 0;
    p1 = L1;
    p2 = L2;
    while (p1)//查找集合1
    {
    find = 0;
    p2 = L2;
    while (p2)//查找集合2中是否包含集合1的元素
    {
    if (p1->data == p2->data){//如果找到表示存在,find赋值为1并跳出
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) return 0;//一旦存在集合1中元素未包含在集合2,表示集合1非集合2子集,返回0停止判断
        p1 = p1->next;
    }
    
    return 1;//集合1全部包含在集合2中,集合1是集合2子集
    

    }

    Node* intersection(Node * L1, Node * L2)//交集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//查找集合1
    {
    p2 = L2;
    while (p2)//查找集合2
    {
    if (p1->data == p2->data){//集合1和集合2都存在的,插入到交集队列
    queuePushBack(&L, p1->data);
    break;
    }

            p2 = p2->next;
        }
        p1 = p1->next;
    }
    
    return L;
    

    }

    Node* Union(Node * L1, Node * L2)//并集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//集合1插入并集集合
    {
    queuePushBack(&L, p1->data);
    p1 = p1->next;
    }

    while (p2)//集合2插入并集集合
    {
        if (IN_SET(L, p2->data)) queuePushBack(&L, p2->data);//集合2插入时判断是否重复
        p2 = p2->next;
    }
    
    return L;
    

    }

    Node* difference(Node * L1, Node * L2)//差集
    {
    Node *L, *p1, *p2;
    int find;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//将集合1减集合2的结果存入差集合
    {
    find = 0;
    p2 = L2;
    while (p2)
    {
    if (p1->data == p2->data){//在集合2中查找集合1中元素是否存在
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) queuePushBack(&L, p1->data);//存在集合1不存在集合2的元素插入差集合
        p1 = p1->next;
    }
    
    return L;
    

    }

    int main()
    {
    FILE *fp = NULL; //文件句柄
    int data, t = 1, i = 0;
    Node *A = NULL, *B = NULL, *C = NULL, *D = NULL, *E = NULL;//队列
    int k = 1;//为0时推出创新
    while (k)
    {
    printf("1、创建队列AB\n");
    printf("2、判断子集关系\n");
    printf("3、求AB交集C\n");
    printf("4、求AB并集D\n");
    printf("5、求AB差集E\n");
    printf("0、退出\n");
    printf("请选择:");
    scanf_s("%d", &i);

        switch (i)
        {
        case 1:
            Emptyqueue(&A);
            Emptyqueue(&B);
            printf("开始创建A:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&A, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            t = 1;
            printf("开始创建B:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&B, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            break;
        case 2:
            if (child(A, B)) printf("集合A是集合B子集\n");
            else printf("集合A不是集合B子集\n");
    
            if (child(B, A)) printf("集合B是集合A子集\n");
            else printf("集合B不是集合A子集\n");
            break;
        case 3:
            Emptyqueue(&C);
            C = intersection(A, B);
            printf("集合AB的交集C:\n");
            queuePrint(C);
            break;
        case 4:
            Emptyqueue(&D);
            D = Union(A, B);
            printf("集合AB的并集D:\n");
            queuePrint(D);
            break;
        case 5:
            Emptyqueue(&E);
            E = difference(A, B);
            printf("集合AB的差E:\n");
            queuePrint(E);
            break;
        case 0:  k = 0; break;
        }
    }
    
    Emptyqueue(&A);
    Emptyqueue(&B);
    Emptyqueue(&C);
    Emptyqueue(&D);
    Emptyqueue(&E);
    return 0;
    

    }#include <stdio.h>
    #include <stdlib.h>
    //数据类型
    #define ElemType int

    typedef struct QueueNode
    {
    ElemType data; //数据域
    struct QueueNode *next; //指针域
    }QueueNode;

    typedef struct LinkQueue
    {
    QueueNode *head; //队头指针
    QueueNode *tail; //队尾指针
    }LinkQueue;

    typedef struct Node {
    int data;
    struct Node *next;
    }Node;

    //创建新节点函数
    Node *CreateNode(int value)
    {
    //为新节点申请空间
    Node new_node = (Node)malloc(sizeof(Node));
    //给新节点赋值
    new_node->data = value;
    //将新节点的next指向空
    new_node->next = NULL;
    return new_node;
    }

    void Emptyqueue(Node **head)//释放队列空间
    {
    Node *cur = *head;
    Node *temp;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    //遍历队列
    while (cur != NULL)
    {
        temp = cur->next;
        free(cur);
        //移动cur,以达到遍历队列的目的
        cur = temp;
    }
    
    *head = NULL;
    

    }

    //顺序打印队列元素
    void queuePrint(Node *head)
    {
    Node *cur;
    if (head == NULL)
    {
    //空队列无需打印
    return;
    }

    cur = head;
    //遍历队列
    while (cur != NULL)
    {
        //打印元素和其对应的地址
        printf("%d\n", cur->data);
        //移动cur,以达到遍历队列的目的
        cur = cur->next;
    }
    printf("\n");
    

    }

    //入队函数,插入链表尾部
    Node *queuePushBack(Node **head, int value)
    {
    Node *cur;
    Node *new_node;
    //非法输入
    if (head == NULL)
    {
    return NULL;
    }
    //空队列
    if (*head == NULL)
    {
    //直接创建一个新的节点完成元素插入
    *head = CreateNode(value);
    return NULL;
    }
    else
    {
    cur = *head;
    //遍历队列,让cur指向最后一个元素
    while (cur->next != NULL)
    {
    cur = cur->next;
    }
    //创建一个新节点
    new_node = CreateNode(value);
    //将最后一个元素的next指向新节点
    cur->next = new_node;
    return new_node;
    }
    }

    //出队函数,从链表头删除
    void queuePop(Node **head)
    {
    //非法输入
    if (head == NULL)
    {
    return;
    }
    //空队列没有可删除的元素
    if (*head == NULL)
    {
    return;
    }
    else
    {
    //创建一个新的指针指向第二个元素
    Node *new_node = (*head)->next;
    //将头结点的next指向空
    (*head)->next = NULL;
    //删除该头结点
    free(*head);
    //将第二个元素设置成新的队头
    *head = new_node;
    }
    return;
    }

    int IN_SET(Node *L, int value)//判断数据是否存在队列中
    {
    int ret = 1;
    Node *p1 = L;
    if (L == NULL) return ret;
    while (p1)
    {
    if (value == p1->data)
    {
    ret = 0;
    break;
    }
    p1 = p1->next;
    }

    return ret;
    

    }

    int INSERT_SET(Node **L, int value)//插入数据
    {
    int ret = 0;
    if (IN_SET(*L, value))//判断是否存在
    {
    queuePushBack(L, value);//插入
    ret = 1;
    }

    return ret;
    

    }

    int child(Node * L1, Node * L2)//判断集合1是否为集合2的子集,是返回1,不是返回0
    {
    Node * L, *p1, *p2;
    int find = 0;
    p1 = L1;
    p2 = L2;
    while (p1)//查找集合1
    {
    find = 0;
    p2 = L2;
    while (p2)//查找集合2中是否包含集合1的元素
    {
    if (p1->data == p2->data){//如果找到表示存在,find赋值为1并跳出
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) return 0;//一旦存在集合1中元素未包含在集合2,表示集合1非集合2子集,返回0停止判断
        p1 = p1->next;
    }
    
    return 1;//集合1全部包含在集合2中,集合1是集合2子集
    

    }

    Node* intersection(Node * L1, Node * L2)//交集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//查找集合1
    {
    p2 = L2;
    while (p2)//查找集合2
    {
    if (p1->data == p2->data){//集合1和集合2都存在的,插入到交集队列
    queuePushBack(&L, p1->data);
    break;
    }

            p2 = p2->next;
        }
        p1 = p1->next;
    }
    
    return L;
    

    }

    Node* Union(Node * L1, Node * L2)//并集
    {
    Node * L, *p1, *p2;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//集合1插入并集集合
    {
    queuePushBack(&L, p1->data);
    p1 = p1->next;
    }

    while (p2)//集合2插入并集集合
    {
        if (IN_SET(L, p2->data)) queuePushBack(&L, p2->data);//集合2插入时判断是否重复
        p2 = p2->next;
    }
    
    return L;
    

    }

    Node* difference(Node * L1, Node * L2)//差集
    {
    Node *L, *p1, *p2;
    int find;
    p1 = L1;
    p2 = L2;
    L = NULL;
    while (p1)//将集合1减集合2的结果存入差集合
    {
    find = 0;
    p2 = L2;
    while (p2)
    {
    if (p1->data == p2->data){//在集合2中查找集合1中元素是否存在
    find = 1;
    break;
    }

            p2 = p2->next;
        }
    
        if (!find) queuePushBack(&L, p1->data);//存在集合1不存在集合2的元素插入差集合
        p1 = p1->next;
    }
    
    return L;
    

    }

    int main()
    {
    FILE *fp = NULL; //文件句柄
    int data, t = 1, i = 0;
    Node *A = NULL, *B = NULL, *C = NULL, *D = NULL, *E = NULL;//队列
    int k = 1;//为0时推出创新
    while (k)
    {
    printf("1、创建队列AB\n");
    printf("2、判断子集关系\n");
    printf("3、求AB交集C\n");
    printf("4、求AB并集D\n");
    printf("5、求AB差集E\n");
    printf("0、退出\n");
    printf("请选择:");
    scanf_s("%d", &i);

        switch (i)
        {
        case 1:
            Emptyqueue(&A);
            Emptyqueue(&B);
            printf("开始创建A:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&A, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            t = 1;
            printf("开始创建B:\n");
            while (t)
            {
                printf("请输入元素:");
                scanf_s("%d", &data);
                if (!INSERT_SET(&B, data)) printf("数据已经存在,插入失败\n");
                else
                {
                    printf("是否继续插入(1继续,0退出):");
                    scanf_s("%d", &t);
                }
            }
            break;
        case 2:
            if (child(A, B)) printf("集合A是集合B子集\n");
            else printf("集合A不是集合B子集\n");
    
            if (child(B, A)) printf("集合B是集合A子集\n");
            else printf("集合B不是集合A子集\n");
            break;
        case 3:
            Emptyqueue(&C);
            C = intersection(A, B);
            printf("集合AB的交集C:\n");
            queuePrint(C);
            break;
        case 4:
            Emptyqueue(&D);
            D = Union(A, B);
            printf("集合AB的并集D:\n");
            queuePrint(D);
            break;
        case 5:
            Emptyqueue(&E);
            E = difference(A, B);
            printf("集合AB的差E:\n");
            queuePrint(E);
            break;
        case 0:  k = 0; break;
        }
    }
    
    Emptyqueue(&A);
    Emptyqueue(&B);
    Emptyqueue(&C);
    Emptyqueue(&D);
    Emptyqueue(&E);
    return 0;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 正在学C++ 2021-07-07 13:03
    关注

    你这个有点复杂呀

    评论
  • qfl_sdu 2021-07-07 14:42
    关注

    代码如下,如有帮助,请采纳一下,谢谢。

    #include <stdio.h>
    #include <stdlib.h>
    
    struct DataSetQueue 
    {
        double val;
        DataSetQueue* next;
    };
    
    
    //显示队列
    void show(struct DataSetQueue* head)
    {
        struct DataSetQueue* p = head;
        while(p)
        {
            printf("%g ",p->val);
            p = p->next;
        }
        printf("\n");
    }
    
    
    //创建队列
    struct DataSetQueue* CreateQueue()
    {
        struct DataSetQueue *head = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
        head->next =0;
        return head;
    }
    
    //复制队列
    struct DataSetQueue* Copy(struct DataSetQueue* head)
    {
        struct DataSetQueue* phead,*t,*p1,*p2;
        phead = CreateQueue();
        phead->val = head->val;
        phead->next = 0;
    
        p1 = phead;
        p2 = head->next;
        while(p2)
        {
            t = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
            t->next = 0;
            t->val = p2->val;
            p1->next = t;
            p1 = t;
            p2 = p2->next;
        }
        return phead;
        
    }
    
    //添加到队列
    void Push(struct DataSetQueue* head,double v)
    {
        struct DataSetQueue* p = head;
        struct DataSetQueue* t = (struct DataSetQueue*)malloc(sizeof(DataSetQueue));
        t->val = v;
        t->next = 0;
        while(p->next)
            p = p->next;
        p->next = t;
    }
    
    //第一个元素弹出队列
    struct DataSetQueue* Pop(struct DataSetQueue* head)
    {
        struct DataSetQueue* p = head;
        head = head->next;
        return p;
    }
    
    //释放空间
    void Release(struct DataSetQueue* head)
    {
        struct DataSetQueue* p = head;
        while(head)
        {
            p = head->next;
            free(head);
            head = p;
        }
    }
    
    
    //判断是否在队列中
    int isInQueue(struct DataSetQueue* head,double v)
    {
        struct DataSetQueue* p = head;
        while(p)
        {
            if(p->val == v)
                return 1;
            else
                p = p->next;
        }
        return 0;
    }
    
    
    //从队列中删除某个元素
    struct DataSetQueue* isInAndDel(struct DataSetQueue* head,double v)
    {
        struct DataSetQueue* p1;
        struct DataSetQueue* t;
        if(head == 0)
            return 0;
        if (head->val == v)
        {
            p1 = head;
            head = head->next;
            free(p1);
            return head;
        }
        p1 = head;
        while(p1->next)
        {
            t = p1->next;
            if(t->val == v)
            {
                p1->next = t->next;
                free(t);
                return head;
            }else
                p1 = p1->next;
        }
        return 0;
    }
    
    
    //子集判定p2是否是p1的子集
    int isSun(struct DataSetQueue* p1,struct DataSetQueue* p2)
    {
        //有问题,需要修改
        struct DataSetQueue* t2;
        struct DataSetQueue* tmp;
        struct DataSetQueue* t1 = Copy(p1);
        t2 = p2;
        while(t2)
        {
            tmp = isInAndDel(t1,t2->val);
            if(tmp)
            {
                t1 = tmp;
                t2 = t2->next;
            }
            else
            {
                Release(t1);
                return 0;
            }
        }
        Release(t1);
        return 1;
    }
    
    //求交集
    struct DataSetQueue* Jiaoji(struct DataSetQueue* p1,struct DataSetQueue* p2)
    {
        struct DataSetQueue* head = 0;
        struct DataSetQueue* tmp;
        struct DataSetQueue* t1 = Copy(p1);
        struct DataSetQueue* t2 = p2;
        while(t2)
        {
            tmp = isInAndDel(t1,t2->val);
            if (tmp)
            {
                t1 = tmp;
                if(head == 0)
                {
                    head = CreateQueue();
                    head->val = t2->val;
                }else
                {
                    Push(head,t2->val);
                }
            }
            t2 = t2->next;
        }
        Release(t1);
        return head;
    }
    
    //求并集
    struct DataSetQueue* Bingji(struct DataSetQueue* p1,struct DataSetQueue* p2)
    {
        struct DataSetQueue* head,*t;
        struct DataSetQueue* tmp;
        struct DataSetQueue* t2 = p2;
        head = Copy(p1);
        while(t2)
        {
            Push(head,t2->val);
            t2 = t2->next;
        }
    
        //减去两者的交集
        t = Jiaoji(p1,p2);
        while(t)
        {
            tmp = isInAndDel(head,t->val);
            if(tmp)
                head = tmp;
            t = t->next;
        }
        Release(t);
        return head;
    }
    //求差在p1中,但不在p2中
    struct DataSetQueue* Chaji(struct DataSetQueue* p1,struct DataSetQueue* p2)
    {
        struct DataSetQueue* tmp;
        struct DataSetQueue* head = Copy(p1);
        struct DataSetQueue* t2 = p2;
        while(t2)
        {
            tmp = isInAndDel(head,t2->val);
            if(tmp)
                head = tmp;
            t2 = t2->next;
        }
        return head;
    }
    
    
    int main()
    {
        int i;
        int a[]={1,2,3,4,5,6,7};
        int b[]={3,6,9,11};
        int c[]={1,2};
        struct DataSetQueue* h1,*h2,*h3,*jj,*bj,*cj;
        h1 = CreateQueue();
        h2 = CreateQueue();
        h3 = CreateQueue();
    
        h1->val = a[0];
        for (i=1;i<7;i++)
            Push(h1,a[i]);
        
        h2->val = b[0];
        for (i=1;i<4;i++)
            Push(h2,b[i]);
    
        h3->val = c[0];
        for(i=1;i<2;i++)
            Push(h3,c[i]);
    
        //显示结合
        printf("集合1:");
        show(h1);
        printf("集合2:");
        show(h2);
        printf("集合3:");
        show(h3);
        
        //判断一个数是否在集合中
        if(isInQueue(h1,3))
            printf("3在集合1中\n");
        else
            printf("3不在集合1中\n");
        if(isInQueue(h2,3))
            printf("3在集合2中\n");
        else
            printf("3不在集合2中\n");
    
        //判断是否属于
        if (isSun(h1,h2))
            printf("集合2属于集合1\n");
        else
            printf("集合2不属于集合1\n");
    
        if (isSun(h1,h3))
            printf("集合3属于集合1\n");
        else
            printf("集合3不属于集合1\n");
    
    
        jj = Jiaoji(h1,h2);
        printf("集合1与集合2的交集:");
        show(jj);
    
        bj = Bingji(h1,h2);
        printf("集合1与集合2的并集:");
        show(bj);
    
        cj = Chaji(h1,h2);
        printf("集合1与集合2的差集:");
        show(cj);
    
        Release(h1);
        Release(h2);
        Release(h3);
        Release(jj);
        Release(bj);
        Release(cj);
        return 0;
    
    }
    
    评论
  • CSDN专家-Time 2021-07-07 10:26
    关注

    可以根据c++的queue库函数修改。

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
  • ¥15 STM32U575 pwm和DMA输出的波形少一段
  • ¥30 android百度地图SDK海量点显示标题
  • ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
  • ¥15 这段代码可以正常运行,打包后无法执行,在执行for内容之前一直不断弹窗,请修改调整
  • ¥15 C语言判断有向图是否存在环路
  • ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
  • ¥20 将resnet50中的卷积替换微ODConv动态卷积
  • ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
  • ¥100 这是什么压缩算法?如何解压?