#include
#include
#include
#define DEBUG 1
#if DEBUG
#define PRINTF printf
#else
#define PRINTF
#endif
#define MAX_VEX (256)
char visited[MAX_VEX+1];
char path_next[MAX_VEX];
int path_record[MAX_VEX];
int g_idx;
typedef struct edge_node
{
int idx;
struct edge_node *next;
}edge_node_t;
typedef struct vertex_node
{
char data;
edge_node_t *first_edge;
}vertex_node_t, vertex_node_list[MAX_VEX];
typedef struct graph
{
vertex_node_list list;
int num_vex;
int num_edge;
}graph_t;
void print_graph(graph_t *g)
{
int i;
edge_node_t *p;
for(i=0; inum_vex; i++)
{
printf("<%d> : %c ", i, g->list[i].data);
if(NULL == g->list[i].first_edge)
printf(" >> NULL");
else
{
for(p=g->list[i].first_edge; p!=NULL; p=p->next)
{
printf(" >> <%d> %c", p->idx, g->list[p->idx].data);
}
}
printf("\n");
}
}
void free_graph(graph_t *g)
{
int i;
edge_node_t *p, *q;
for(i=0; inum_vex; i++)
{
p = g->list[i].first_edge;
while(p)
{
q = p->next;
free(p);
p = q;
}
}
}
int create_graph(graph_t *g)
{
int i, j, k;
edge_node_t *p, *q;
int ret;
PRINTF("input num of vertex and num of edge : ");
ret = scanf("%d %d\n", &(g->num_vex), &(g->num_edge));
PRINTF("ret = %d, num_vex : %d, num_edge : %d\n", ret, g->num_vex, g->num_edge);
PRINTF("input data of vertex:\n");
for(i=0; inum_vex; i++)
{
PRINTF("vertex %d : ", i+1);
ret = scanf("%c\n", &(g->list[i].data));
PRINTF("ret = %d, %d : %d\n", ret, i, g->list[i].data);
g->list[i].first_edge = NULL;
}
for(k=0; knum_edge; k++)
{
PRINTF("input the index of (vi, vj) : ");
ret = scanf("%d %d\n", &i, &j);
PRINTF("ret : %d, i : %d, j : %d\n", ret, i, j);
p = (edge_node_t *)malloc(sizeof(edge_node_t));
memset(p, 0x0, sizeof(edge_node_t));
p->idx = j;
#if 0
p->next = g->list[i].first_edge;
g->list[i].first_edge = p;
#else
if(g->list[i].first_edge == NULL)
g->list[i].first_edge = p;
else
{
for(q=g->list[i].first_edge; q->next!=NULL; q=q->next);
q->next = p;
}
#endif
p = (edge_node_t *)malloc(sizeof(edge_node_t));
memset(p, 0x0, sizeof(edge_node_t));
p->idx = i;
#if 0
p->next = g->list[j].first_edge;
g->list[j].first_edge = p;
#else
if(g->list[j].first_edge == NULL)
g->list[j].first_edge = p;
else
{
for(q=g->list[j].first_edge; q->next!=NULL; q=q->next);
q->next = p;
}
#endif
}
}
edge_node_t *neighbour(graph_t *g, int idx)
{
edge_node_t *p;
p = g->list[idx].first_edge;
while(NULL != p)
{
if(visited[p->idx] == 1)
{
p = p->next;
}
else
{
if(-1 == path_next[idx])
{
while(p!=NULL && visited[p->idx]==1)
p = p->next;
return p;
}
else if(p->idx == path_next[idx])
{
p = p->next;
while(p!=NULL && visited[p->idx]==1)
p = p->next;
return p;
}
else
{
p = p->next;
}
}
}
}
void all_path(graph_t *g, int start, int end)
{
int i, cur_vex;
edge_node_t *p;
while(g_idx >= 0)
{
cur_vex = path_record[g_idx];
if(cur_vex == end)
{
PRINTF("%d -> %d : ", start, end);
for(i=0; i<=g_idx; i++)
{
if(i == 0)
PRINTF("<%d> %c", path_record[i], g->list[path_record[i]].data);
else
PRINTF(" >> <%d> %c", path_record[i], g->list[path_record[i]].data);
}
PRINTF("\n");
visited[end] = 0;
path_record[g_idx] = -1;
path_next[end] = -1;
g_idx--;
}
else
{
p = neighbour(g, cur_vex);
if(p != NULL)
{
path_next[cur_vex] = p->idx;
g_idx++;
path_record[g_idx] = p->idx;
visited[p->idx] = 1;
}
else
{
visited[cur_vex] = 0;
path_record[g_idx] = -1;
path_next[cur_vex] = -1;
g_idx--;
}
}
}
}
int main()
{
graph_t g;
int start, end;
int ret, i;
memset(&g, 0x0, sizeof(g));
#if DEBUG
freopen("data.txt", "r", stdin);
#endif
create_graph(&g);
#if DEBUG
PRINTF("[%s][%d] print_graph : \n", __FUNCTION__, __LINE__);
print_graph(&g);
#endif
for(i=0; i0;
path_next[i] = -1;
path_record[i] = -1;
}
g_idx = 0;
PRINTF("input start and end node index : ");
ret = scanf("%d %d\n", &start, &end);
PRINTF("ret : %d, start : %d, end : %d\n", ret, start, end);
path_record[g_idx] = start;
visited[start] = 1;
all_path(&g, start, end);
free_graph(&g);
return 0;
}