BFS实现C抛出分段故障

时间:2018-06-10 07:14:05

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

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

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

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

主要是我猜它不会在里面循环(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)

答案得到解决,谢谢大家的帮助。

1 个答案:

答案 0 :(得分:1)

这部分可能导致段错:

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

您已经测试current不是NULL,但current->next可能是。{/ p>

我相信行current=current->next应该只是在while循环结束时移动。

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