C - 使用递归打印链接列表

时间:2017-07-18 12:31:58

标签: c recursion linked-list

我需要在C中编写一个函数,给定一个指向链表的指针,将打印出Python语法中的元素:对于由1,2,3,4和5组成的列表,该函数将打印出[1,2,3,4,5]。

我尝试编写如下代码:

struct node {
    struct node *next;
    int       data;
};

void print_list(struct node *list) {
    printf("[");
    if (list == NULL) {
        printf("]");
    } else {
        printf("%d", list->data);
        if (list->next != NULL) {
            printf(", ");
        }
        print_list(list->next);
    }
}

输出如下:[1,[2,[3,[4,[5 []

我理解每次函数调用自身时," ["将被打印。有没有办法打印" ["只有在第一次调用函数时?

4 个答案:

答案 0 :(得分:5)

您可以创建用于打印大括号的包装函数。在打印之间,调用实际的递归函数,如下所示:

void print_list(struct node *list) {
    putchar('[');
    print_list_helper(list);
    putchar(']');

void print_list_helper(struct node *list) {
    if (list != NULL)
        printf("%d", list->data);
        if (list->next != NULL) {
            printf(", ");
        }
        print_list_helper(list->next);
    }
}

编辑: 作为Felix Palmen的pointed outstatic变量是一个选项,但不是最好的。它仅在您第一次调用该函数时有效。之后,计数器必须重置,这不容易做,并使递归函数不纯。

使用static

的示例
#include <stdio.h>

void foo() {
    static int x = 0;
    printf("x In foo: %d\n", x);
    x++;
}

int main() {
    int i;
    for(i = 0; i < 5; i++) {
        foo();
    }

    return 0;
}

打印:

 $ ./a.out 
x In foo: 0
x In foo: 1
x In foo: 2
x In foo: 3
x In foo: 4

静态整数在函数调用之间保留其值。因此,您可以在第一次调用递归函数时使用它,但每次遇到基本情况时都需要重置它。

static的另一个例子:

void foo() {
    static int x = 0; // initialization (done ONCE!)
    printf("x In foo: %d\n", x);
    x++;
    if(x%2 == 0) x = 0; // reassignment (done as many times as you like)
}
... // everything else is the same

这给出了:

$ ./a.out   
x In foo: 0
x In foo: 1
x In foo: 0
x In foo: 1
x In foo: 0

答案 1 :(得分:1)

你可以使用静态整数。

struct node {
    struct node *next;
    int       data;
};

void print_list(struct node *list) {
    static int print_start = 0;
    if (print_start == 0) {
        printf("[");
    }
    if (list == NULL) {
        printf("]");
        print_start = 0;
    } else {
        print_start++;
        printf("%d", list->data);
        if (list->next != NULL) {
            printf(", ");
        }
        print_list(list->next);
    }
}

答案 2 :(得分:0)

使用全局变量并将代码更改为

int count=0;
void print_list(struct node *list) {
    if(count==0)
        printf("[");
    if (list == NULL) {
        printf("]");
    } else {
        printf("%d", list->data);
        if (list->next != NULL) {
            printf(", ");
        }
        print_list(list->next);
    }
count++;

}

答案 3 :(得分:0)

添加一个附加参数,显示它是从外部还是递归调用的。最优雅的方式,但会花费你一些钱。

相关问题