#include "dfsbfs.h"
#include "graph_list.h"
#include <stdio.h>
#include <stdlib.h>
void initQueue(queue* q)
{
q=(queue*)malloc(sizeof(q));
q->first=NULL;
q->rear=NULL;
}
void enqueue(queue* q, vertex* v)
{
if(isQEmpty(q))
{
q->first=v;
q->rear=v;
}
else
{
q->rear->nextV=v;
q->rear=v;
}
}
vertex* dequeue(queue* q)
{
printf("enter");
if(isQEmpty(q))
{
fputs("error dequeue", stderr);
exit(-1);
}
else
{
vertex* returnNode=q->first;
q->first=q->first->nextV;
printf("dequeue???");
return returnNode;
}
}
int isQEmpty(queue* q)
{
return q->first==NULL;
}
void bfs(graph_list* graph, name startVertex)
{
queue q;
initQueue(&q);
vertex *v;
graph->verticies[startVertex].color=GRAY;
graph->verticies[startVertex].distance=0;
graph->verticies[startVertex].parent=NULL;
printf("ffffffff");
printf("bbbbbbbbb");
printf("aaaaaaaa");
visitVertex(&(graph->verticies[startVertex]));
enqueue(&q, &(graph->verticies[startVertex]));
while(!isQEmpty(&q))
{
v=dequeue(&q); //SEG FAULT HERE
/*
node* current=v->next;
while(current!=NULL)
{
graph->verticies[current->nodeName].color=GRAY;
graph->verticies[current->nodeName].distance=(v->distance)+1;
graph->verticies[current->nodeName].parent=v;
visitVertex(&(graph->verticies[current->nodeName]));
enqueue(&q, &(graph->verticies[current->nodeName]));
}
v->color=BLACK;
*/
}
}
void visitVertex(vertex* v)
{
printf("Visited Vertex: %c \n", (v->vertexName)+65);
printf("hello");
}
从这段代码中,我得到了分段错误&#39;在代码中:
v=dequeue(&q);
如果我写dequeue(&q);
而非v=dequeue(&q);
,则没有错误。
但是,我不知道后者为什么会给我一个错误。指针变量returnNode
具有q->frist
的地址。因此,在返回后,v应该有q->frist
的地址。
但为什么这会给我分段错误?
答案 0 :(得分:3)
您的<div class="outer">
<p>
This longer text will be vertically aligned.
</p>
</div>
<div class="outer">
<p>
This longer text will be vertically aligned and centered. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
错了。当您传递指向initQueue
实例的指针时,不需要分配queue
,您只需要初始化变量。它应该是这样的:
queue
注意:虽然您不需要,但是对void initQueue(queue* q)
{
q->first=NULL;
q->rear=NULL;
}
的调用是错误的。说malloc
将分配指针大小的东西作为malloc(sizeof(q)
的内容。你应该在那个问题上说q
。