Tag: queue

使用BFS计算源和顶点之间的距离

我试图使用邻接列表来计算从源顶点到其他顶点的距离。 我正在使用队列来完成这个,但是我得到除了源之外的每个顶点的距离为-1,但我不确定为什么会发生这种情况 #include #include #include “input_error.h” #define VertexToSearch 1 typedef struct edge { int vertexIndex; struct edge *edgePtr; } edge; typedef struct vertex { int vertexKey; struct edge *edgePtr; int visited; int distance; } vertex; typedef struct queue { struct vertex v; struct queue* next; }queue; int vertexCount = 0; struct vertex graph[]; void load_file(char*); […]

优先级队列C.

我正在尝试使用队列数组创建priority queue ,数组的每个索引都是优先级。 我尝试了以下解决方案, 队列数据类型包含数组llist, Queue *q_create(int size) { struct queue *p; struct q_head *h; int i; if ((p = (struct queue *)malloc(sizeof(struct queue))) != NULL) { p->size = size; for (i = 0; i llist[i]); h->head = NULL; h->tail = NULL; } } return p; } 我h = &(p->llist[i]);条线感到困惑: h = &(p->llist[i]); 我在想llist[i] = […]

如何在迷宫中打印从源到目标的BFS路径

我正在尝试实现BFS,以便找到迷宫中从源到目标的最短路径。 我遇到的问题是我无法打印路径,它在迷宫中打印了’*’,但是如何在不打印所有被访问节点的情况下从BFS的前辈中提取路径? 这是我编译的代码: #include #include #include struct coord{ //This is a struct I’ll use to store coordinates int row; int col; }; //———QUEUE.C——-// struct TQueue{ struct coord *A; int QUEUE_MAX; }; typedef struct TQueue *Queue; Queue initQueue(int size){ // Initialize the queue Queue Q = malloc(sizeof(struct TQueue)); Q->A = malloc(size*sizeof(struct coord)); Q->QUEUE_MAX = size+1; Q->A[0].row […]

为什么sys / queue.h中的双向链表保持前一个下一个元素的地址?

我正在从FreeBSD学习sys/queue.h ,我有一个问题: 在sys/queue.h , LIST_ENTRY定义如下: #define LIST_ENTRY(type) \ struct { \ struct type *le_next; /* next element */ \ struct type **le_prev; /* address of previous next element */ \ } 为什么它保持前一个下一个元素的地址 ( struct type **le_prev )而不是像struct type *le_prev那样的前一个元素 ?