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;
}
}
int enqueue(int value)
{
if ( rear >= MAXQUEUE )
return -1;
rear++;
queue[rear] = value;
}
int dequeue()
{
if ( front == rear )
return -1;
front++;
return queue[front];
}
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] == 0 )
{
enqueue(ptr->vertex);
visited[ptr->vertex] = 1;
printf("[%d] ",ptr->vertex);
}
ptr = ptr->nextnode;
}
}
}
void dfs(int current)
{
graph ptr;
visited[current] = 1;
printf("[%d] ",current);
ptr = head[current].nextnode;
while ( ptr != NULL )
{
if ( visited[ptr->vertex] == 0 )
dfs(ptr->vertex);
ptr = ptr->nextnode;
}
}
void main()
{clrscr();
while(1)
{
char c,a;
graph ptr;
int i;
int node[60][2] = { {1, 10}, {10, 1},
{2, 10}, {10, 2},
{2, 3}, {3, 2},
{3, 4}, {4, 3},
{3, 12}, {12, 3},
{4, 13}, {13, 4},
{4, 5}, {5, 4},
{5, 6}, {6, 5},
{5, 7}, {7, 5},
{7, 8}, {8, 7},
{9, 10}, {10, 9},
{10, 11}, {11, 10},
{11, 14}, {14, 11},
{11, 12}, {12, 11},
{12, 15}, {15, 12},
{12, 13}, {13, 12},
{13, 16}, {16, 13},
{14, 17}, {17, 14},
{14, 18}, {18, 14},
{15, 19}, {19, 15},
{16, 20}, {20, 16},
{17, 18}, {18, 17},
{18, 23}, {23, 18},
{18, 19}, {19, 18},
{19, 23}, {23, 19},
{19, 24}, {24, 19},
{19, 20}, {20, 19},
{20, 21}, {21, 20},