mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 15:29:53 +08:00
Several bug fixes and improvements. (#887)
* fix the bugs of C code. * Add a header figure. * Improve the definition of tree node height.
This commit is contained in:
@@ -20,12 +20,12 @@ int recur(int n) {
|
||||
/* 使用迭代模拟递归 */
|
||||
int forLoopRecur(int n) {
|
||||
int stack[1000]; // 借助一个大数组来模拟栈
|
||||
int top = 0;
|
||||
int top = -1; // 栈顶索引
|
||||
int res = 0;
|
||||
// 递:递归调用
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 通过“入栈操作”模拟“递”
|
||||
stack[top++] = i;
|
||||
stack[1 + top++] = i;
|
||||
}
|
||||
// 归:返回结果
|
||||
while (top >= 0) {
|
||||
@@ -64,6 +64,9 @@ int main() {
|
||||
res = recur(n);
|
||||
printf("\n递归函数的求和结果 res = %d\n", res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
printf("\n使用迭代模拟递归求和结果 res = %d\n", res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
printf("\n尾递归函数的求和结果 res = %d\n", res);
|
||||
|
||||
|
||||
@@ -224,9 +224,9 @@ void removeVertex(GraphAdjList *graph, unsigned int index) {
|
||||
// 将顶点前移
|
||||
for (int i = index; i < graph->size - 1; i++) {
|
||||
graph->vertices[i] = graph->vertices[i + 1]; // 顶点前移
|
||||
graph->vertices[i]->pos--; // 所有前移的顶点索引值减1
|
||||
graph->vertices[i]->pos--; // 所有前移的顶点索引值减 1
|
||||
}
|
||||
graph->vertices[graph->size - 1] = 0; // 将被删除顶点的位置置 0
|
||||
graph->vertices[graph->size - 1] = 0;
|
||||
graph->size--;
|
||||
// 释放内存
|
||||
freeVertex(vet);
|
||||
|
||||
Reference in New Issue
Block a user