queue.h
#ifndef H_QUEUE
#define H_QUEUE
#include <stdlib.h>
typedef struct _nodes {
vertex *data;
struct _nodes *next;
} node;
typedef struct _queue {
int length;
node *front;
node *end;
} queue;
int push(queue *, vertex *);
int pop(queue *);
int push(queue *q, vertex *v)
{
node *element;
element = (node *)malloc(sizeof(node));
element->data = v;
element->next = NULL;
if (q->length == 0)
{
q->front = element;
q->end = element;
}
else
{
node *temp;
temp = q->front;
while (temp->next != NULL)
temp = temp->next;
temp->next = element;
q->end = element;
}
q->length++;
}
int pop(queue *q)
{
if (q->length == 0) return -1;
node *temp;
temp = q->front;
q->front = q->front->next;
q->length--;
if (q->length == 1)
q->end = NULL;
free(temp);
}
#endif
bfs.h
#ifndef H_BFS
#define H_BFS
#include "graph.h"
#include "queue.h"
#include <stdio.h>
int bfs(graph *);
int bfs(graph *g)
{
queue q;
q.length = 0;
push(&q, g->node_list[0]);
int i;
vertex *temp;
temp = g->node_list[0];
while (q.length > 0)
{
if (temp->alength > 0)
{
for (i = 0; i < temp->alength; i++)
push(&q, temp->adjacency_nodes[i]);
temp = temp->adjacency_nodes[0];
}
pop(&q);
}
}
#endif
