C BFS上的无限循环

C BFS上的无限循环,c,infinite-loop,breadth-first-search,C,Infinite Loop,Breadth First Search,我正在尝试制作一个程序,查找节点B是否属于从节点a开始的子树。我用C编写了代码,并自行实现了队列机制,因为我使用BFS遍历树。问题是,我的代码运行到一个无限循环中,表示我的队列已满,甚至没有 代码: #include <stdlib.h> #include <stdio.h> #define MAXSIZE 6 typedef struct Queue { int capacity; int size; int front; int r

我正在尝试制作一个程序,查找节点B是否属于从节点a开始的子树。我用C编写了代码,并自行实现了队列机制,因为我使用BFS遍历树。问题是,我的代码运行到一个无限循环中,表示我的队列已满,甚至没有

代码:

#include <stdlib.h>
#include <stdio.h>


#define MAXSIZE 6
typedef struct Queue
{
    int capacity;
    int size;
    int front;
    int rear;
    int * elements;
}Queue;
Queue * createQueue(int maxElements)
{
    Queue *q;
    if (NULL != (q = (Queue *)malloc(sizeof(Queue))))
    {
        q->elements = (int*)malloc(maxElements * sizeof(int));
        q->size = 0;
        q->capacity = maxElements;
        q->front = 0;
        q->rear = -1;
        return q;
    }
}


void dequeue(Queue *q)
{
    if (0 == q->size)
    {
        printf("Queue is empty\n");
        return;
    }
    else
    {
        q->size--;
        q->front++;
        if (q->front == q->capacity)
        {
            q->front = 0;
        }
    }
}


int front(Queue *q)
{
    if (q->size == 0)
    {
        printf("queue is empty\n");
        exit(0);
    }
    return q->elements[q->front];
}


void enqueue(Queue *q, int element)
{
    if (q->size == q->capacity)
    {
        printf("queue is full \n");
    }

    else
    {
        q->size++;
        q->rear++;
        if (q->rear == q->capacity)
        {
            q->rear = 0;
        }
        q->elements[q->rear] = element;
    }
    return;
}



void readInput(int A[],int B[],int M[][MAXSIZE],int N)
{
    FILE * fp;
    int row, col;
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            M[i][j] = 0;
    if (0 == fopen_s(&fp,"input.txt", "r")) 
    {
        fscanf_s(fp, "%d", &N);
        for (int i = 0; i < N; i++)
            fscanf_s(fp,"%d ", &A[i]);
        for (int i = 0; i < N; i++)
            fscanf_s(fp,"%d ", &B[i]);


        for (int i = 0; i < N; i++)
        {
            fscanf_s(fp, "%d %d", &row,&col);
            M[row][col] = 1;
        }

    }

}

bool vBelongsToUSubtree(int M[][MAXSIZE], int *u, int* v)
{
    bool belongs = false, visited[MAXSIZE];
    for (short i = 0; i < MAXSIZE; i++)visited[i] = false;
    visited[*u] = true;
    Queue *q = createQueue(MAXSIZE);
    enqueue(q, *u);
    printf("%d\n", front(q));
    int node;
    while (0 != q->size)
    {
        node = front(q);
        dequeue(q);
        for (int i = 1; i < MAXSIZE; i++)
        {
            if (1 == M[node][i] and false == visited[i])
            {
                enqueue(q, node);
                visited[node] = true;
                if (node == *v)return true;
            }
            //printf("!\n");
        }
        //printf("%d\n", node);

    }
    /*for (int i = 0; i < q->size; i++)
    {
        printf("%d \n", front(q));
    }

*/
    return belongs;
}

int main()
{
    int A[100], B[100], M[MAXSIZE][MAXSIZE], N=0;
    readInput(A, B, M, N);
    for(int i=1;i<=MAXSIZE;i++)
        for(int j=i;j<=MAXSIZE;j++)
             if(vBelongsToUSubtree(M, &i, &j))printf("yes");
             else printf("not");
    system("pause");

    return 0;
}
#包括
#包括
#定义最大尺寸6
typedef结构队列
{
国际能力;
整数大小;
内锋;
内部后部;
int*元素;
}排队;
队列*createQueue(int maxElements)
{
队列*q;
if(NULL!=(q=(队列*)malloc(sizeof(队列)))
{
q->elements=(int*)malloc(maxElements*sizeof(int));
q->size=0;
q->容量=最大元素;
q->front=0;
q->rear=-1;
返回q;
}
}
无效出列(队列*q)
{
如果(0==q->大小)
{
printf(“队列为空\n”);
返回;
}
其他的
{
q->尺寸--;
q->front++;
如果(q->front==q->capacity)
{
q->front=0;
}
}
}
int前端(队列*q)
{
如果(q->size==0)
{
printf(“队列为空\n”);
出口(0);
}
返回q->elements[q->front];
}
无效队列(队列*q,int元素)
{
如果(q->size==q->capacity)
{
printf(“队列已满\n”);
}
其他的
{
q->size++;
q->rear++;
如果(q->后==q->容量)
{
q->rear=0;
}
q->元素[q->后]=元素;
}
返回;
}
无效读取输入(int A[],int B[],int M[][MAXSIZE],int N)
{
文件*fp;
int row,col;
对于(int i=0;i<5;i++)
对于(int j=0;j<5;j++)
M[i][j]=0;
如果(0==fopen_s(&fp,“input.txt”,“r”))
{
fscanf_s(fp、%d、&N);
对于(int i=0;isize)
{
节点=前端(q);
出列(q);
对于(int i=1;isize;i++)
{
printf(“%d\n”,前面(q));
}
*/
返回属于;
}
int main()
{
int A[100],B[100],M[MAXSIZE][MAXSIZE],N=0;
读取输入(A、B、M、N);

对于(inti=1;i,正如注释中所指出的,您现有的代码存在一些问题。在预期它能够正常工作之前,您应该解决这些问题

这个答案只针对你所问的无限循环

在评论中还指出:


如果语句中的i或j:
If(vBelongsToUSubtree(M,&i,&j))printf(“yes”);
没有得到正确的控制,它们可能永远不会满足
for(int i=1;编辑您的帖子,在您提供的代码段中指出发生无限循环的地方。(可能是一个内嵌的
//注释
)如果语句中的
i
j
如果(vBelongsToUSubtree(M,&i,&j))printf(“yes”);
未正确控制,则它们可能永远不会满足
for(int i=1;i@OctavianCotfasa此函数无效读取输入(int A[],int B[],int M[][MAXSIZE],int N)没有意义。没有使用它的参数N。也不清楚为什么在函数中使用幻数5。@CTAVIA Cotfasa函数createQueue有未定义的行为。我建议删除该问题并重新重写程序。如果
if(vBelongsToUSubtree(M, &i, &j))printf("yes");
enqueue(q, *u);
void enqueue(Queue *q, int element)