Skip to content

Commit 14a9733

Browse files
committed
[code] refactoring 63~74
1 parent f240fff commit 14a9733

File tree

13 files changed

+664
-777
lines changed
  • 000-inc
  • 063-汉诺塔问题
  • 064-哈夫曼编码
  • 065-图的深度优先遍历
  • 066-图的广度优先遍历
  • 067-求解最优交通路径
  • 068-八皇后问题
  • 069-骑士巡游
  • 070-用栈设置密码
  • 071-魔王语言翻译
  • 072-火车车厢重排
  • 073-队列实例
  • 074-K阶斐波那契序列

13 files changed

+664
-777
lines changed

000-inc/inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifdef CLS_AND_GETCH
1212
#ifndef _INC_CONIO
1313
#include <conio.h> // _getch
14+
#define getch _getch
1415
#endif
1516
#endif // end #ifdef CLS_AND_GETCH
1617

063-汉诺塔问题/63.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
/*//////////////////////////////////////////////////////////////*/
2-
/* 汉诺塔问题 */
3-
/*//////////////////////////////////////////////////////////////*/
1+
//////////////////////////////////////////////////////////////////
2+
// 汉诺塔问题 //
3+
//////////////////////////////////////////////////////////////////
44
#include <stdio.h>
5+
#include <stdlib.h> // exit
56

6-
/* hanoil 子程序,实现将n个盘子从a移动到c */
7-
void hanoil(int n, char a, char b, char c)
8-
{
9-
if (n == 1) /* 递归调用的出口,n=1 */
10-
printf(" >> Move Plate No.%d from Stick %c to Stick %c.\n", n, a, c);
11-
else
12-
{
13-
hanoil(n - 1, a, c, b); /* 递归调用 */
14-
printf(" >> Move Plate No.%d from Stick %c to Stick %c.\n", n, a, c);
7+
8+
long step = 0;
9+
void print_step(int n, char a, char c) {
10+
printf("%4d >> Move Plate No.%d from Stick %c to Stick %c.\n", ++step, n, a, c);
11+
}
12+
13+
// hanoil 子程序,实现将n个盘子从a移动到c
14+
void hanoil(int n, char a, char b, char c) {
15+
if (n == 1) { // 递归调用的出口,n=1
16+
print_step(n, a, c);
17+
} else {
18+
hanoil(n - 1, a, c, b); // 递归调用
19+
print_step(n, a, c);
1520
hanoil(n - 1, b, a, c);
1621
}
1722
}
1823

19-
/****************************** 主程序******************************/
20-
21-
void main()
22-
{
24+
int main() {
2325
int n;
2426
char a = 'A';
2527
char b = 'B';
2628
char c = 'C';
27-
clrscr();
28-
printf("This is a hanoil program.\nPlease input number of the plates:\n");
29+
30+
printf("This is a hanoil program.\nPlease input number of the plates: ");
2931
scanf("%d", &n);
30-
if (n <= 0)
31-
{
32+
if (n <= 0) {
3233
puts("n must no less than 1!");
3334
exit(1);
3435
}
3536
puts("The steps of moving plates are:");
37+
step = 0;
3638
hanoil(n, a, b, c);
37-
puts("\n Press any key to quit...");
38-
getch();
39+
40+
return 0;
3941
}

064-哈夫曼编码/64.c

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,75 @@
33
#define MAXSYMBS 30
44
#define MAXNODE 59
55

6-
typedef struct
7-
{
6+
typedef struct {
87
int weight;
98
int flag;
109
int parent;
1110
int lchild;
1211
int rchild;
13-
} huffnode;
12+
} HuffNode;
1413

15-
typedef struct
16-
{
14+
typedef struct {
1715
int bits[MAXSYMBS];
1816
int start;
19-
} huffcode;
17+
} HuffCode;
2018

21-
main()
22-
{
23-
huffnode huff_node[MAXNODE];
24-
huffcode huff_code[MAXSYMBS], cd;
19+
20+
int main() {
21+
HuffNode huff_node[MAXNODE];
22+
HuffCode huff_code[MAXSYMBS], cd;
2523
int i, j, m1, m2, x1, x2, n, c, p;
26-
/* char symbs[MAXSYMBS],symb; */
27-
/*数组huff_node初始化*/
28-
clrscr();
29-
printf("please input the leaf num of tree:\n");
24+
25+
printf("please input the leaf num of tree: ");
3026
scanf("%d", &n);
31-
for (i = 0; i < 2 * n - 1; i++)
32-
{
27+
for (i = 0; i < 2 * n - 1; i++) {
28+
// 数组 huff_node 初始化
3329
huff_node[i].weight = 0;
3430
huff_node[i].parent = 0;
35-
huff_node[i].flag = 0;
31+
huff_node[i].flag = 0;
3632
huff_node[i].lchild = -1;
3733
huff_node[i].rchild = -1;
3834
}
3935

4036
printf("please input the weight of every leaf\n");
41-
for (i = 0; i < n; i++)
42-
scanf("%d", &huff_node[i].weight);
43-
/*构造哈夫曼树*/
44-
for (i = 0; i < n - 1; i++)
45-
{
37+
for (i = 0; i < n; i++) scanf("%d", &huff_node[i].weight);
38+
39+
// 构造哈夫曼树
40+
for (i = 0; i < n - 1; i++) {
4641
m1 = m2 = MAX;
4742
x1 = x2 = 0;
48-
for (j = 0; j < n + i; j++)
49-
{
50-
if (huff_node[j].weight < m1 && huff_node[j].flag == 0)
51-
{
43+
for (j = 0; j < n + i; j++) {
44+
if (huff_node[j].weight < m1 && huff_node[j].flag == 0) {
5245
m2 = m1;
5346
x2 = x1;
5447
m1 = huff_node[j].weight;
5548
x1 = j;
56-
}
57-
else if (huff_node[j].weight < m2 && huff_node[j].flag == 0)
58-
{
49+
} else if (huff_node[j].weight < m2 && huff_node[j].flag == 0) {
5950
m2 = huff_node[j].weight;
6051
x2 = j;
6152
}
6253
}
6354

64-
huff_node[x1].parent = n + i; /*将找出的两棵子树合并为一棵子树*/
55+
huff_node[x1].parent = n + i; // 将找出的两棵子树合并为一棵子树
6556
huff_node[x2].parent = n + i;
6657
huff_node[x1].flag = 1;
6758
huff_node[x2].flag = 1;
6859
huff_node[n + i].weight = huff_node[x1].weight + huff_node[x2].weight;
6960
huff_node[n + i].lchild = x1;
7061
huff_node[n + i].rchild = x2;
7162
}
72-
/*求字符的哈夫曼编码*/
7363

74-
for (i = 0; i < n; i++)
75-
{
64+
// 求字符的哈夫曼编码
65+
for (i = 0; i < n; i++) {
7666
cd.start = n;
7767
c = i;
7868
p = huff_node[c].parent;
79-
while (p != 0)
80-
{
81-
if (huff_node[p].lchild == c)
82-
69+
while (p != 0) {
70+
if (huff_node[p].lchild == c) {
8371
cd.bits[cd.start] = 0;
84-
else
72+
} else {
8573
cd.bits[cd.start] = 1;
74+
}
8675
cd.start = cd.start - 1;
8776
c = p;
8877
p = huff_node[p].parent;
@@ -92,14 +81,14 @@ main()
9281
huff_code[i].bits[j] = cd.bits[j];
9382
huff_code[i].start = cd.start;
9483
}
95-
/*输出字符的哈夫曼编码*/
84+
85+
// 输出字符的哈夫曼编码
9686
puts("The Hafman code are:");
97-
for (i = 0; i < n; i++)
98-
{
99-
for (j = huff_code[i].start; j <= n; j++)
87+
for (i = 0; i < n; i++) {
88+
for (j = huff_code[i].start; j <= n; j++) {
10089
printf("%10d", huff_code[i].bits[j]);
90+
}
10191
printf("\n");
10292
}
103-
puts("Press any key to quit...");
104-
getch();
93+
return 0;
10594
}

065-图的深度优先遍历/65.c

Lines changed: 66 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,90 @@
1-
/*/////////////////////////////////////////////////////////////*/
2-
/* 图的深度优先遍历 */
3-
/*/////////////////////////////////////////////////////////////*/
1+
/////////////////////////////////////////////////////////////////
2+
// 图的深度优先遍历 //
3+
/////////////////////////////////////////////////////////////////
44
#include <stdlib.h>
55
#include <stdio.h>
66

7-
struct node /* 图顶点结构定义 */
8-
{
9-
int vertex; /* 顶点数据信息 */
10-
struct node *nextnode; /* 指下一顶点的指标 */
7+
struct node { // 图顶点结构定义
8+
int vertex; // 顶点数据信息
9+
struct node *nextnode; // 指下一顶点的指标
1110
};
12-
typedef struct node *graph; /* 图形的结构新型态 */
13-
struct node head[9]; /* 图形顶点数组 */
14-
int visited[9]; /* 遍历标记数组 */
11+
typedef struct node *Graph; // 图形的结构新型态
12+
struct node head[9]; // 图形顶点数组
13+
int visited[9]; // 遍历标记数组
1514

16-
/********************根据已有的信息建立邻接表********************/
17-
void creategraph(int node[20][2], int num) /*num指的是图的边数*/
18-
{
19-
graph newnode; /*指向新节点的指针定义*/
20-
graph ptr;
21-
int from; /* 边的起点 */
22-
int to; /* 边的终点 */
15+
//*******************根据已有的信息建立邻接表*******************
16+
void creategraph(int node[20][2], int num) { //num指的是图的边数
17+
Graph newnode; // 指向新节点的指针定义
18+
Graph ptr;
19+
int from; // 边的起点
20+
int to; // 边的终点
2321
int i;
2422

25-
for (i = 0; i < num; i++) /* 读取边线信息,插入邻接表*/
26-
{
27-
from = node[i][0]; /* 边线的起点 */
28-
to = node[i][1]; /* 边线的终点 */
23+
for (i = 0; i < num; i++) { // 读取边线信息,插入邻接表
24+
from = node[i][0]; // 边线的起点
25+
to = node[i][1]; // 边线的终点
2926

30-
/* 建立新顶点 */
31-
newnode = (graph)malloc(sizeof(struct node));
32-
newnode->vertex = to; /* 建立顶点内容 */
33-
newnode->nextnode = NULL; /* 设定指标初值 */
34-
ptr = &(head[from]); /* 顶点位置 */
35-
while (ptr->nextnode != NULL) /* 遍历至链表尾 */
36-
ptr = ptr->nextnode; /* 下一个顶点 */
37-
ptr->nextnode = newnode; /* 插入节点 */
27+
// 建立新顶点
28+
newnode = (Graph)malloc(sizeof(struct node));
29+
newnode->vertex = to; // 建立顶点内容
30+
newnode->nextnode = NULL; // 设定指标初值
31+
ptr = &(head[from]); // 顶点位置
32+
while (ptr->nextnode != NULL) // 遍历至链表尾
33+
ptr = ptr->nextnode; // 下一个顶点
34+
ptr->nextnode = newnode; // 插入节点
3835
}
3936
}
4037

41-
/********************** 图的深度优先搜寻法********************/
38+
//********************* 图的深度优先搜寻法*******************
39+
void dfs(int current) {
40+
Graph ptr;
4241

43-
void dfs(int current)
44-
{
45-
graph ptr;
46-
47-
visited[current] = 1; /* 记录已遍历过 */
48-
printf("vertex[%d]\n", current); /* 输出遍历顶点值 */
49-
ptr = head[current].nextnode; /* 顶点位置 */
50-
while (ptr != NULL) /* 遍历至链表尾 */
51-
{
52-
if (visited[ptr->vertex] == 0) /* 如过没遍历过 */
53-
dfs(ptr->vertex); /* 递回遍历呼叫 */
54-
ptr = ptr->nextnode; /* 下一个顶点 */
42+
visited[current] = 1; // 记录已遍历过
43+
printf("vertex[%d]\n", current); // 输出遍历顶点值
44+
ptr = head[current].nextnode; // 顶点位置
45+
while (ptr != NULL) { // 遍历至链表尾
46+
if (visited[ptr->vertex] == 0) // 如过没遍历过
47+
dfs(ptr->vertex); // 递回遍历呼叫
48+
ptr = ptr->nextnode; // 下一个顶点
5549
}
5650
}
5751

58-
/****************************** 主程序******************************/
59-
60-
void main()
61-
{
62-
graph ptr;
63-
int node[20][2] = {{1, 2}, {2, 1}, /* 边线数组 */
64-
{1, 3},
65-
{3, 1},
66-
{1, 4},
67-
{4, 1},
68-
{2, 5},
69-
{5, 2},
70-
{2, 6},
71-
{6, 2},
72-
{3, 7},
73-
{7, 3},
74-
{4, 7},
75-
{4, 4},
76-
{5, 8},
77-
{8, 5},
78-
{6, 7},
79-
{7, 6},
80-
{7, 8},
81-
{8, 7}};
52+
int main() {
53+
Graph ptr;
54+
int node[20][2] = { // 边线数组
55+
{1, 2}, {2, 1},
56+
{1, 3}, {3, 1},
57+
{1, 4}, {4, 1},
58+
{2, 5}, {5, 2},
59+
{2, 6}, {6, 2},
60+
{3, 7}, {7, 3},
61+
{4, 7}, {4, 4},
62+
{5, 8}, {8, 5},
63+
{6, 7}, {7, 6},
64+
{7, 8}, {8, 7}
65+
};
8266
int i;
83-
clrscr();
84-
for (i = 1; i <= 8; i++) /* 顶点数组初始化 */
85-
{
86-
head[i].vertex = i; /* 设定顶点值 */
87-
head[i].nextnode = NULL; /* 指针为空 */
88-
visited[i] = 0; /* 设定遍历初始标志 */
67+
68+
for (i = 1; i <= 8; i++) { // 顶点数组初始化
69+
head[i].vertex = i; // 设定顶点值
70+
head[i].nextnode = NULL; // 指针为空
71+
visited[i] = 0; // 设定遍历初始标志
8972
}
9073

91-
creategraph(node, 20); /* 建立邻接表 */
74+
creategraph(node, 20); // 建立邻接表
9275
printf("Content of the gragh's ADlist is:\n");
93-
for (i = 1; i <= 8; i++)
94-
{
95-
printf("vertex%d ->", head[i].vertex); /* 顶点值 */
96-
ptr = head[i].nextnode; /* 顶点位置 */
97-
while (ptr != NULL) /* 遍历至链表尾 */
76+
for (i = 1; i <= 8; i++) {
77+
printf("vertex%d ->", head[i].vertex); // 顶点值
78+
ptr = head[i].nextnode; // 顶点位置
79+
while (ptr != NULL) // 遍历至链表尾
9880
{
99-
printf(" %d ", ptr->vertex); /* 印出顶点内容 */
100-
ptr = ptr->nextnode; /* 下一个顶点 */
81+
printf(" %d ", ptr->vertex); // 印出顶点内容
82+
ptr = ptr->nextnode; // 下一个顶点
10183
}
102-
printf("\n"); /* 换行 */
84+
printf("\n");
10385
}
10486
printf("\nThe end of the dfs are:\n");
105-
dfs(1); /* 打印输出遍历过程 */
106-
printf("\n"); /* 换行 */
107-
puts(" Press any key to quit...");
108-
getch();
87+
dfs(1); // 打印输出遍历过程
88+
printf("\n"); // 换行
89+
return 0;
10990
}

0 commit comments

Comments
 (0)