|
/ * ---------------------------------------- * /
/ * Main program: After creating the graphics, print the traversal content. * /
/ * ---------------------------------------- * /
void main ()
{clrscr ();
while (1)
{
char c, a;
graph ptr;
int i;
int node [60] [2] = {{1, 10}, {10, 1}, / * edge array * /
{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},
{22, 23}, {23, 22},
{24, 25}, {25,24}
};
clrscr ();
printf ("\n\n\n");
printf ("/ * --------------------------------------------- --------- * /\n ");
printf ("/ * Welcome to use this program * /\n");
printf ("/ * --------------------------------------------- --------- * /\n ");
printf ("This program is an algorithmic demonstration of the traversal of related graphs,\n");
printf ("If there are any shortcomings, please forgive me!\n\n");
printf ("Please ask if you want to run the following program:\n\n");
printf ("Depth and breadth traversal of the graph? Y / N?\n");
c = getch ();
if (c! = 'y'&&'Y')
exit (0);
clrscr ();
printf ("\n\n");
printf ("Please note the following code for each city:\n\n");
printf ("1: Urumqi; 2: Hohhot; 3: Beijing; 4: Tianjin; 5: Shenyang;\n");
printf ("6: Dalian; 7: Changchun; 8: Harbin; 9: Xining; 10: Lanzhou;\n");
printf ("11: Xi'an; 12: Zhengzhou; 13: Xuzhou; 14: Chengdu; 15: Wuhan;\n");
printf ("16: Shanghai; 17: Kunming; 18: Guiyang; 19: Zhuzhou; 20: Nanchang;\n");
printf ("21: Fuzhou; 22: Nanning; 23: Liuzhou; 24: Guangzhou; 25: Shenzhen.\n");
for (i = 1; i <= 25; i ++)
{
head [i] .vertex = i; / * set the vertex value * /
head [i] .nextnode = NULL; / * clear graph indicator * /
visited [i] = 0; / * set the initial value of traversal * /
}
creategraph (node, 60); / * Create graph * /
printf ("The content of the adjacency list of the graph:\n");
for (i = 1; i <= 25; i ++)
{if (i% 3 == 0) printf ("\n");
printf ("Vertex% d =>", head [i] .vertex); / * Vertex value * /
ptr = head [i] .nextnode; / * vertex position * /
while (ptr! = NULL) / * Traverse to the end of the list * /
{
printf ("% d", ptr-> vertex); / * print out vertex content * /
ptr = ptr-> nextnode; / * next vertex * /
}
}
printf ("\n\n");
printf ("Please select the operation you need\n");
printf ("1, the breadth of the graph is traversed first, please enter: 'g' or 'G'\n");
printf ("2. Depth-first traversal of graphics, please enter: 's' or' S'\n");
c = getch ();
switch (c)
{
case'g ': case'G':
printf ("\n please input the starting point you need:\n");
scanf ("% d",&i);
clrscr ();
printf ("\n\n");
printf ("Please note the following code for each city:\n\n");
printf ("1: Urumqi; 2: Hohhot; 3: Beijing; 4: Tianjin; 5: Shenyang;\n");
printf ("6: Dalian; 7: Changchun; 8: Harbin; 9: Xining; 10: Lanzhou;\n");
printf ("11: Xi'an; 12: Zhengzhou; 13: Xuzhou; 14: Chengdu; 15: Wuhan;\n");
printf ("16: Shanghai; 17: Kunming; 18: Guiyang; 19: Zhuzhou; 20: Nanchang;\n");
printf ("21: Fuzhou; 22: Nanning; 23: Liuzhou; 24: Guangzhou; 25: Shenzhen.\n");
printf ("The breadth of the graph is preferably traversed at the top point of the content:\n");
bfs (i); / * Print out the traversal process * /
printf ("\n"); / * newline * /
break;
case's ': case'S':
printf ("\n, please enter the starting point you need:\n");
scanf ("% d",&i);
clrscr ();
printf ("\n\n");
printf ("Please note the following code for each city:\n\n");
printf ("1: Urumqi; 2: Hohhot; 3: Beijing; 4: Tianjin; 5: Shenyang;\n");
printf ("6: Dalian; 7: Changchun; 8: Harbin; 9: Xining; 10: Lanzhou;\n");
printf ("11: Xi'an; 12: Zhengzhou; 13: Xuzhou; 14: Chengdu; 15: Wuhan;\n");
printf ("16: Shanghai; 17: Kunming; 18: Guiyang; 19: Zhuzhou; 20: Nanchang;\n");
printf ("21: Fuzhou; 22: Nanning; 23: Liuzhou; 24: Guangzhou; 25: Shenzhen.\n");
printf ("The depth of the graph is traversed first, the top point content:\n");
dfs (i); / * Print out the traversal process * /
printf ("\n"); / * newline * /
break;
}
printf ("\n please ask if you want to continue: y / n");
a = getch ();
if (a! = 'y'&&'Y')
exit (0);
}
} |
|