感觉c学的很不好,但是为什么看似很浅显的东西,竟然有问题,好晕。
#include<stdlib.h>
#include<stdio.h>
#define MAXQUEUE 70
struct node
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph;
struct node head[61];
int visited[61];
int queue[MAXQUEUE];
int front=-1;
int rear=-1;
void creategraph(int *node,int num)
{
graph newnode;
graph ptr;
int from;
int to;
int i;
for(i=0;i<num;i++)
{
from=node[i*2];
to=node[i*2+1];
newnode=(graph)malloc(sizeof(struct node));
newnode->vertex=to;
newnode->nextnode=NULL;
ptr=&(head[from]);
while(ptr->nextnode!=NULL)
ptr=ptr->nextnode;
ptr->nextnode=newnode;
}
}
void bfs(int current)
{
graph ptr;
enqueue(current);
visited[current]=1;
printf("%d",current);
while(front!=rear)
{
current=dequeue();
ptr=head[current].nextnode;
while(ptr!=NULL)
{
if(visited[ptr->vertex]);
{
enqueue(ptr->vertex);
visited[ptr->vertex]=1;
printf("%d",ptr->vertex);
}
ptr=ptr->nextnode;
}
}
}
截图:
没法弄了,烦。。。