c - C语言使用队列的BFS遍历程序

标签 c algorithm data-structures graph

我现在正在研究图,并且正在使用C。当我用邻接表表示图时,我需要一个队列来进行BFS遍历。但是,我在代码方面遇到了一些问题 - 我不确定我是否很好地理解了 bfs 与队列遍历的概念。

我粘贴了下面注释的代码,希望它是可读的。有人可以检查一下,或者至少提供一些关于如何以正确的方式实现这一目标的信息吗?

程序崩溃并且还显示段错误

#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int rear;
    int front;
    int capacity;
    int* array;
};
struct adjlistnode{
    int dest;
    struct adjlistnode* next;
};
struct adjlist{
    struct adjlistnode* head;
};
struct graph{
    int V;
    struct adjlist* array;
};
int visited[100];

struct Queue* createqueue(int capacity){
    struct Queue* queue=(struct Queue*)malloc(sizeof(struct Queue));
    queue->rear = -1;
    queue->front = -1;
queue->capacity=capacity;
queue->array=(int*)malloc(sizeof(int)*capacity);
return queue;
}
int isempty(struct Queue* queue){
    return(queue->front==-1 && queue->rear==-1);
}
void enqueue(struct Queue* queue,int data){
    if(isempty(queue)){
        queue->rear=0;
        queue->front=0;
        queue->array[queue->rear]=data;
        printf("%d",queue->array[queue->rear]);
        return;
    }
queue->rear=(queue->rear+1)%queue->capacity;
queue->array[queue->rear]=data;
} 
int dequeue(struct Queue* queue){
    if(isempty(queue))
        return -1;
    int temp=queue->front;
    queue->front=(queue->front+1)%queue->capacity;
    return (queue->array[temp]); 
}
int isfront(struct Queue* queue){
    return(queue->array[queue->front]);
}
/// GRAPH FUNCTIONS
struct adjlistnode* getnewnode(int dest){
    struct adjlistnode* newnode =(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
    newnode->dest=dest;
    newnode->next=NULL;
    return newnode;
}
struct graph* creategraph(int V){
    struct graph* G = (struct graph*)malloc(sizeof(struct graph));
    G->V=V;
    G->array=(struct adjlist*)malloc(V*sizeof(struct adjlist));
    for(int i=0;i<V;i++){
        G->array[i].head=NULL;
    }
 return G;
}
void addedge(struct graph* G,int src ,int dest){
    struct adjlistnode* newnode=getnewnode(dest);

    newnode->next=G->array[src].head;
    G->array[src].head=newnode; 

    newnode=getnewnode(src);

    newnode->next=G->array[dest].head;
    G->array[dest].head=newnode;
}
void printgraph(struct graph* G){
    for(int i=0;i<G->V;i++){
        struct adjlistnode* temp=G->array[i].head;
        while(temp!=NULL){
            printf("%d",temp->dest);
            temp=temp->next;
        }
        printf("\n");
    }
}
void bfs(struct graph* G,struct Queue* queue,int startvertex){


    enqueue(queue,startvertex);


    while(!isempty(queue)){
       int u=dequeue(queue);
       visited[u]=1;
       printf(" \n %d ",u);
       struct adjlistnode* temp=G->array[u].head;
       while(temp){
         if(visited[temp->dest]==0){
             visited[temp->dest]=1;
             enqueue(queue,temp->dest);
          }
       temp=temp->next;
       }

    }

}
void bfstraversal(struct graph* G,struct Queue *queue){
    int i;
    for(i=0;i<G->V;i++)
        visited[i]=0;
    for(i=0;i<G->V;i++){
        if(!visited[i]){
            bfs(G,queue,i);
        }
    }
}
 int main(){
    struct Queue* queue=createqueue(100);


    struct graph* G=creategraph(5);
    addedge(G,1,2);
    addedge(G,1,1);
    printgraph(G);
    bfstraversal(G,queue);
  //  printf("\n%d",dequeue(queue));
}

最佳答案

您的 bfs 工作正常,正如人们指出的那样,您的队列 isEmpty() 函数有一个缺陷。它应该检查 2 个条件,

  1. 到目前为止,还没有元素入队或出队。为此,条件是
    (队列->前==-1 && 队列->后==-1)

  2. 所有入队的元素均已出队, 例如,在队列中,您已入队 2 、 3 ,然后后部=0,前部=1。现在你已经出列了 2 个元素 这使得队列为空,rear=2,front=1。这就是后部超过前部的情况。您必须检查前索引和后索引之间的差值是否等于 1 的情况。

    ((队列->容量-(队列->前队列->后))%队列->容量) == 1

还有一个建议

排队时,最好检查队列是否已达到其容量。

关于c - C语言使用队列的BFS遍历程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52510239/

相关文章:

python - 使用预计算针对特定用例优化 Python 算法

java - 如何定期更改 Canvas 对象的颜色?

java - ComparisonCountingSort伪代码难度

algorithm - 给定一个单词列表,以高效的方式找到从该单词生成的所有可能的单词

c# - C# 中是否有任何排序列表允许在我们向其中添加元素时进行排序?

c - 如何在 C 中正确分配结构数组 HashMap 中的项

c - 在 C 中,结构数组如何在内存中查找

c++ - OpenGL:如何设置渲染器以不显示阴影、灯光和颜色阴影?

c++ - 红黑树实现

将 CFStringRef 转换为 char *