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

毕业设计驿站 其他问答 1

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

回复

共1条回复 我来回复
  • 源码码头网
    这个人很懒,什么都没有留下~
    评论
    #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;
    }
    
    0条评论

发表回复

登录后才能评论