【问题标题】:BFS implementation in C throwing segmentation fault [closed]C中的BFS实现抛出分段错误[关闭]
【发布时间】:2018-11-19 17:46:18
【问题描述】:

这是一个 C 语言的 BFS 程序。在进入循环“while(Q!=NULL)”后的函数“bfs(struct Graph *G,int v)”中,循环中只发生了一次迭代(意味着它只打印第一个顶点的相邻边作为输入输入)...下次检查时会抛出分段错误(当输入下一个顶点作为输入时)。

在输出的第一个示例中:
当我输入 0(作为第一个顶点)和 1(作为它的边缘)时,它只打印...当它进入下一个输入 1(作为第一个顶点)和 2(作为它的边缘)它正在抛出分段错误

对于输出中的第二个示例类似:
当我输入 0(作为第一个顶点)和 1 和 2(作为它的边缘)时,它只打印那个,当我进入下一个顶点时,它的抛出段错误......

主要是我猜它不会在 while(Q!=NULL) 内循环。

谁能帮我解决这个问题?

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

#define MAX 25

struct Queue
{
   int data;
   struct Queue *next;
};

struct node
{
  int verticenum;
  struct node *next;
};

struct list
{
 struct node *head;
};

struct Graph
{
  int V;
  int E;
  struct list *Adj;
};


struct Queue* Enqueue(struct Queue* head,int data)
{
   struct Queue* p=malloc(sizeof(struct Queue));
   struct Queue* curr=head;
   p->data=data;
   if(head==NULL)
    {
      p->next=NULL;
      head=p;
      return head;
    }
   else
    {
      while(curr->next!=NULL)
      {
        curr=curr->next;
      }
    curr->next=p;
    p->next=NULL;
   return head;
}
}


struct Queue* Dequeue(struct Queue *head, int *x)
{
   struct Queue *current=head;

   if(head==NULL)
   {
     printf("Queue is empty\n");
   }
  else
   {
    head=head->next;
    *x=current->data;
    free(current);
    return head;
}
}



struct Graph *addelementinlist()
{
  int i,x,y;

  struct Graph *G = malloc(sizeof(struct Graph));

  printf("Enter the vertices and Edges : ");
  scanf("%d %d",&G->V,&G->E);

  G->Adj=malloc(sizeof(struct list) * G->V);

  //Read the vertices;
  for(i=0;i<G->V;i++)
  {
    G->Adj[i].head=(struct node*)malloc(sizeof(struct node));
    G->Adj[i].head->verticenum=i;
    G->Adj[i].head->next=NULL;
  }

  //Read the edges
  for(i=0;i<G->E;i++)
  {
    printf("Enter the source and destinatiion : ");
    scanf("%d %d",&x,&y);
    struct node *temp=malloc(sizeof(struct node));
    struct node *temp1=malloc(sizeof(struct node));

    struct node *curr=malloc(sizeof(struct node));
    curr=G->Adj[x].head;
    temp->verticenum=y;
    if(curr==NULL)
    {
       temp->next=NULL;
       curr->next=temp;
    }
    else
    {
        while(curr->next!=NULL)
         {
             curr=curr->next;
         }
    temp->next=NULL;
    curr->next=temp;
    }

//Incase of undirected other one 
    curr=G->Adj[y].head;
    temp1->verticenum=x;
    if(curr==NULL)
    {
       temp1->next=NULL;
       curr->next=temp1;
    }
    else
    {
        while(curr->next!=NULL)
         {
             curr=curr->next;
         }
    temp1->next=NULL;
    curr->next=temp1;
    }
  }

 return G;
}


void bfs(struct Graph *G, int v)
{
  int visited[MAX],u,a,w;

  visited[v]=1;
  u=v;

  printf("Visit u\t%d\n",u); 
  struct Queue *Q = malloc(sizeof(struct Queue));
  Q = Enqueue(Q,u);

  while(Q!=NULL)
  {

   Q = Dequeue(Q,&u);
   printf("Vertice u : %d\n",u);
   struct node *current=G->Adj[u].head;
   while(current!=NULL)
   {
     current=current->next;
     printf("current->verticenum : %d\n",current->verticenum);
     w=current->verticenum;
     Q = Enqueue(Q,w);
     printf("Adjascent vertices w : %d\n",w);
     if(visited[w]==0)
      {
        visited[w]=1;
      }
   }

  }
}

void bft(struct Graph *G)
{
  int visited[MAX],i;
  for(i=0;i<G->V;i++)
   {
      visited[i]=0;
   }
  for(i=0;i<G->V;i++)
  {
     if(visited[i]==0)
      {
        bfs(G,i);
      }
  }
}


void printgraph(struct Graph *G)
{
  int i;

  for(i=0;i<G->V;i++)
  {
     struct node *temp = G->Adj[i].head;
      while(temp!=NULL)
       {
          printf("%d",temp->verticenum);
          temp=temp->next;
          printf("->");
       }
    printf("NULL\n");
  }
}

int main()
{
  struct Graph *p = addelementinlist();
  bft(p);
  printgraph(p);
} 

Example 1(output):
Enter the vertices and Edges : 4 3
Enter the source and destinatiion : 0 1
Enter the source and destinatiion : 1 2
Enter the source and destinatiion : 2 3
Visit u 0
Vertice u : 0
current->verticenum : 1
Adjascent vertices w : 1
Segmentation fault (core dumped)



Enter the vertices and Edges : 4 3
Enter the source and destinatiion : 0 1
Enter the source and destinatiion : 0 2
Enter the source and destinatiion : 1 2
Visit u 0
Vertice u : 0
current->verticenum : 1
Adjascent vertices w : 1
current->verticenum : 2
Adjascent vertices w : 2
Segmentation fault (core dumped)

答案已经解决,谢谢大家的帮助。

【问题讨论】:

  • 在这些情况下使用调试器特别有效。试试看,之后你将无法工作。
  • printf("current->verticenum : %d\n",current->verticenum); w=当前->顶点;这是我遇到段错误的确切地方..我不确定我是错误访问还是什么....
  • 这一行中唯一可能出错的是访问由地址指向的元素。检查那个地址。
  • yaa 地址显示为空。 162 printf(“当前->顶点:%d\n”,当前->顶点); (gdb) p current->verticenum 无法访问地址 0x0 处的内存 (gdb) p &current->verticenum $1 = (int *) 0x0 但我不知道如何解决这个问题...
  • 下面的答案解释了为什么你得到 NULL

标签: c linked-list queue breadth-first-search


【解决方案1】:

这部分可能会导致段错误:

   while(current!=NULL)
   {
     current=current->next;
     printf("current->verticenum : %d\n",current->verticenum);

您已经测试过current 不是NULLcurrent-&gt;next 可能是。

我认为 current=current-&gt;next 行应该简单地移动到 while 循环的末尾。

另外,不要忘记测试您是否已经访问过某个节点。如果图形是循环的,这可能会创建一个无限循环。

【讨论】:

  • yaa 这个显示分段错误 printf("current->verticenum : %d\n",current->verticenum);
  • 我是在回答您的问题还是您需要更多帮助?我相信 current=current-&gt;next 行应该简单地在 while 循环结束时移动,但我还没有测试过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2019-12-31
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多