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



```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的函数

问题在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

本来是咋样的