酒煮青梅392 2023-07-13 09:54 采纳率: 65.2%
浏览 60
已结题

顺序表代码请修正,出现未知错误



```c
#include <stdio.h>‘

#include <stdlib.h>



#define MAXSIZE 100



typedef struct node

{

    int data[MAXSIZE];

    int length;

} Seqlist, *PseqList;



PseqList Creat_Seqlist(void)

{

    PseqList PL;

    PL = (PseqList)malloc(sizeof(Seqlist));

    if (PL)

        PL->length = 0;

    return (PL);

}



int length_Seqlist(PseqList PL)

{

    return (PL->length);

}



int location_SeqList(PseqList PL, int x)

{

    int i = 0;

    while (i < PL->length && PL->data[i] != x)

        i++;



    if (i >= PL->length)

        return 0;

    else

        return (i + 1);

}



int insert_Seqlist(PseqList PL, int i, int x)

{

    int j;

    if (!PL)

    {

        printf("不存在的");

        return 0;

    }



    if (PL->length >= MAXSIZE)

    {

        printf("表太小");

        return 0;

    }



    if (i < 1 || i > PL->length + 1)

    {

        printf("你输入错啦");

        return 0;

    }



    for (j = PL->length - 1; j >= i - 1; j--)

    {

        PL->data[j + 1] = PL->data[j];

    }



    PL->data[i - 1] = x;

    PL->length++;

    return 1;

}



int Delete_Seqlist(PseqList PL, int i)

{

    int j;

    if (!PL)

    {

        printf("不存在的");

        return -1;

    }



    if (i < 1 || i > PL->length)

    {

        printf("你输入错啦");

        return 0;

    }



    for (j = i; j <= PL->length - 1; j++)

    {

        PL->data[j - 1] = PL->data[j];

    }



    PL->length--;

    return 1;

}



void ajiaob(PseqList A, PseqList B)

{

    int i;

    while(i<A->length)

    {

        if(!location_SeqList(B,A->data[i]))

        Delete_Seqlist(A,i+1);

        else

        i++;

    }

}



void atongb(PseqList A, PseqList B)

{

    int i;

    for (i = 0; i < B->length; i++)

    {

        if (!location_SeqList(A, B->data[i]))

        {

            insert_Seqlist(A, A->length + 1, B->data[i]);

        }

    }

}



int main()

{

    PseqList A, B;

    A = Creat_Seqlist();

    B = Creat_Seqlist();

    int m, n, i;

    int a1[6] = {1, 2, 3, 6, 4, 5}, b1[4] = {3, 88, 9, 60};



    for (m = 0; m < 6; m++)

    {

        insert_Seqlist(A, m + 1, a1[m]);

    }



    for (n = 0; n < 4; n++)

    {

        insert_Seqlist(B, n + 1, b1[n]);

    }



    PseqList A_B = Creat_Seqlist();

    PseqList AUB = Creat_Seqlist();



    // 计算A-B

    for (i = 0; i < A->length; i++)

    {

        insert_Seqlist(A_B, i + 1 , A->data[i]);

    }

    ajiaob(A_B, B);



    // 计算AUB

    for (i = 0; i < A->length; i++)

    {

        insert_Seqlist(AUB, i + 1, A->data[i]);

    }

    atongb(AUB, B);



    printf("A-B: ");

    for (i = 0; i < A_B->length; i++)

    {

        printf("%d ", A_B->data[i]);

    }



    printf("\nAUB: ");

    for (i = 0; i < AUB->length; i++)

    {

        printf("%d ", AUB->data[i]);

    }



    return 0;
}

img

为啥是这样,这是一个求A-B和AUB的函数

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-07-13 21:42
    关注

    问题在void ajiaob(PseqList A, PseqList B) 函数里,两处见注释,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 100
    
    typedef struct node
    {
        int data[MAXSIZE];
        int length;
    
    } Seqlist, *PseqList;
    
    PseqList Creat_Seqlist(void)
    {
        PseqList PL;
        PL = (PseqList)malloc(sizeof(Seqlist));
        if (PL)
            PL->length = 0;
        return (PL);
    }
    
    int length_Seqlist(PseqList PL)
    {
        return (PL->length);
    }
    
    int location_SeqList(PseqList PL, int x)
    {
        int i = 0;
        while (i < PL->length && PL->data[i] != x)
            i++;
        if (i >= PL->length)
            return 0;
        else
            return (i + 1);
    }
    
    int insert_Seqlist(PseqList PL, int i, int x)
    {
        int j;
        if (!PL)
        {
            printf("不存在的");
            return 0;
        }
        if (PL->length >= MAXSIZE)
        {
            printf("表太小");
            return 0;
        }
        if (i < 1 || i > PL->length + 1)
        {
            printf("你输入错啦");
            return 0;
        }
        for (j = PL->length - 1; j >= i - 1; j--)
        {
            PL->data[j + 1] = PL->data[j];
        }
        PL->data[i - 1] = x;
        PL->length++;
        return 1;
    }
     
    int Delete_Seqlist(PseqList PL, int i)
    {
        int j;
        if (!PL)
        {
            printf("不存在的");
            return -1;
        }
        if (i < 1 || i > PL->length)
        {
            printf("你输入错啦");
            return 0;
        }
        for (j = i; j <= PL->length - 1; j++)
        {
            PL->data[j - 1] = PL->data[j];
        }
        PL->length--;
        return 1;
    }
    
    void ajiaob(PseqList A, PseqList B)
    {
        int i = 0; //int i; 修改
        while(i<A->length)
        {
            if(location_SeqList(B, A->data[i])) // 修改
            // if(!location_SeqList(B, A->data[i]))
                Delete_Seqlist(A,i+1);
            else
                i++;
        }
    }
    
    void atongb(PseqList A, PseqList B)
    {
        int i;
        for (i = 0; i < B->length; i++)
        {
            if (!location_SeqList(A, B->data[i]))
            {
                insert_Seqlist(A, A->length + 1, B->data[i]);
            }
        }
    }
    
    int main()
    {
        PseqList A, B;
        A = Creat_Seqlist();
        B = Creat_Seqlist();
        int m, n, i;
        int a1[6] = {1, 2, 3, 6, 4, 5}, b1[4] = {3, 88, 9, 60};
        for (m = 0; m < 6; m++)
        {
            insert_Seqlist(A, m + 1, a1[m]);
        }
        for (n = 0; n < 4; n++)
        {
            insert_Seqlist(B, n + 1, b1[n]);
        }
        PseqList A_B = Creat_Seqlist();
        PseqList AUB = Creat_Seqlist();
    
        // 计算A-B
        for (i = 0; i < A->length; i++)
        {
            insert_Seqlist(A_B, i + 1 , A->data[i]);
        }
        ajiaob(A_B, B);
    
        // 计算AUB
        for (i = 0; i < A->length; i++)
        {
            insert_Seqlist(AUB, i + 1, A->data[i]);
        }
        atongb(AUB, B);
    
        printf("A-B: ");
        for (i = 0; i < A_B->length; i++)
        {
            printf("%d ", A_B->data[i]);
        }
    
        printf("\nAUB: ");
        for (i = 0; i < AUB->length; i++)
        {
            printf("%d ", AUB->data[i]);
        }
        return 0;
    }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月22日
  • 已采纳回答 7月14日
  • 修改了问题 7月13日
  • 创建了问题 7月13日

悬赏问题

  • ¥20 求下下面这个数据结构代码
  • ¥15 路由器考试怎么办,有懂行的吗 ,eNSP
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来
  • ¥15 java 判断某个数 区间是否存在
  • ¥15 appium控制多个雷电模拟器问题
  • ¥15 C# iMobileDevice
  • ¥15 谁会做这个啊#ensp#Boson NetSim
  • ¥15 如何编写针对TPS6503320FRGE型号的电源管理芯片的编程代码?
  • ¥15 设计简单目录管理系统,要满足以下内容