#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
typedef struct node
{
int vertex;
int edge[4];
}Node;
typedef struct list{
int data;
struct list *next;
}List;
List arr[8];
Node graph[8];
int front=0,rear=0;
int queue[30];
int flag[8] = {0};
void Bfs(int v);
int pop();
void push(int item);
int main()
{
int i,j;
int current = 0;
List* temp, *cur;
for(i=0;i<8;i++)
{
graph[i].vertex=i;
for(j=0;j<4;j++){
graph[i].edge[j] = -1;
}
}
graph[0].edge[0]=1;
graph[0].edge[1]=2;
graph[1].edge[0]=0;
graph[1].edge[1]=3;
graph[1].edge[2]=4;
graph[2].edge[0]=0;
graph[2].edge[1]=5;
graph[2].edge[2]=6;
graph[3].edge[0]=1;
graph[3].edge[1]=7;
graph[4].edge[0]=1;
graph[4].edge[1]=7;
graph[5].edge[0]=2;
graph[5].edge[1]=7;
graph[6].edge[0]=2;
graph[6].edge[1]=7;
graph[7].edge[0]=3;
graph[7].edge[1]=4;
graph[7].edge[2]=5;
graph[7].edge[3]=6;
for(i=0;i<8;i++)
{
j=0;
while(j < 4)
{
if(graph[i].edge[j] == -1){
j++;
continue;
}
cur = &arr[i];
temp = (List*)malloc(sizeof(List));
temp->data = graph[i].edge[j];
temp->next = NULL;
while(cur->next != NULL){
cur = cur->next;
}
cur->next = temp;
j++;
}
}
printf("Recuresive version : ");
Bfs(0);
printf("\n");
}
void Bfs(int v)
{
int i;
if(flag[v]==1)
return ;
flag[v]=1;
printf("%5d",v);
for(i=0;i<4;i++)
{
if(graph[v].edge[i]==-1)
continue;
else
push(graph[v].edge[i]);
}
Bfs(pop());
Bfs(pop());
}
void push(int item)
{
queue[rear++]=item;
}
int pop()
{
if(rear==front)
{
return -1;
}
return queue[front++];
}
'IT' 카테고리의 다른 글
dfs (0) | 2014.05.14 |
---|---|
그래프 전은 자료구조 아니죠 ㅎㅎ (0) | 2014.05.14 |
이 역시 그래프 (0) | 2014.05.14 |
그래프 관련인듯 (0) | 2014.05.14 |
제목은 나중에 붙이는 걸로 (0) | 2014.05.14 |