title: BFS traversal program in c using queue
body: <p>I'm studying graphs at the moment, and I'm using C. When I represent a graph with an adjacency list, I need a queue for a BFS traversal. However, I'm having some issues with the code - I'm not sure if I grasped the concept of a bfs traversal with queues well.</p>
  
  <p>I pasted the commented code below, I hope it's readable. Can someone check it out, or at least provide some info on how to pull this off the right way?</p>
  
  <p>program is crashing and also showing segmentation fault   </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  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-&gt;rear = -1;
      queue-&gt;front = -1;
  queue-&gt;capacity=capacity;
  queue-&gt;array=(int*)malloc(sizeof(int)*capacity);
  return queue;
  }
  int isempty(struct Queue* queue){
      return(queue-&gt;front==-1 &amp;&amp; queue-&gt;rear==-1);
  }
  void enqueue(struct Queue* queue,int data){
      if(isempty(queue)){
          queue-&gt;rear=0;
          queue-&gt;front=0;
          queue-&gt;array[queue-&gt;rear]=data;
          printf("%d",queue-&gt;array[queue-&gt;rear]);
          return;
      }
  queue-&gt;rear=(queue-&gt;rear+1)%queue-&gt;capacity;
  queue-&gt;array[queue-&gt;rear]=data;
  } 
  int dequeue(struct Queue* queue){
      if(isempty(queue))
          return -1;
      int temp=queue-&gt;front;
      queue-&gt;front=(queue-&gt;front+1)%queue-&gt;capacity;
      return (queue-&gt;array[temp]); 
  }
  int isfront(struct Queue* queue){
      return(queue-&gt;array[queue-&gt;front]);
  }
  /// GRAPH FUNCTIONS
  struct adjlistnode* getnewnode(int dest){
      struct adjlistnode* newnode =(struct adjlistnode*)malloc(sizeof(struct adjlistnode));
      newnode-&gt;dest=dest;
      newnode-&gt;next=NULL;
      return newnode;
  }
  struct graph* creategraph(int V){
      struct graph* G = (struct graph*)malloc(sizeof(struct graph));
      G-&gt;V=V;
      G-&gt;array=(struct adjlist*)malloc(V*sizeof(struct adjlist));
      for(int i=0;i&lt;V;i++){
          G-&gt;array[i].head=NULL;
      }
   return G;
  }
  void addedge(struct graph* G,int src ,int dest){
      struct adjlistnode* newnode=getnewnode(dest);
  
      newnode-&gt;next=G-&gt;array[src].head;
      G-&gt;array[src].head=newnode; 
  
      newnode=getnewnode(src);
  
      newnode-&gt;next=G-&gt;array[dest].head;
      G-&gt;array[dest].head=newnode;
  }
  void printgraph(struct graph* G){
      for(int i=0;i&lt;G-&gt;V;i++){
          struct adjlistnode* temp=G-&gt;array[i].head;
          while(temp!=NULL){
              printf("%d",temp-&gt;dest);
              temp=temp-&gt;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-&gt;array[u].head;
         while(temp){
           if(visited[temp-&gt;dest]==0){
               visited[temp-&gt;dest]=1;
               enqueue(queue,temp-&gt;dest);
            }
         temp=temp-&gt;next;
         }
  
      }
  
  }
  void bfstraversal(struct graph* G,struct Queue *queue){
      int i;
      for(i=0;i&lt;G-&gt;V;i++)
          visited[i]=0;
      for(i=0;i&lt;G-&gt;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));
  }
  </code></pre>