灵耀下沉思中的成长
2021-09-06 21:40
采纳率: 57.1%
浏览 37

顺序表的存储结构设计运用

1.写出顺序表的存储结构;

2.写出顺序表常用操作(函数);

3.编写实现集合交与并的两个函数;

4.编写测试函数,测试集合的交与并;

5.编写main函数运行程序。
  • 收藏

2条回答 默认 最新

  • qfl_sdu 2021-09-06 22:54
    已采纳

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

    #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;
     
    }
    
    
    已采纳该答案
    1 打赏 评论
  • CSDN专家-link 2021-09-06 21:56
    #define maxlen 1000
    typedef struct _table
    {
          int data[maxlen];
          int len;
    }table;
    
    打赏 评论

相关推荐 更多相似问题