gpt4 book ai didi

c - 尝试打印二叉树内容时的函数声明

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:33 24 4
gpt4 key购买 nike

我一直在尝试编写一个函数来打印二叉树的内容,其中最左边的分支最靠近 A,最右边的分支最靠近 Z。然而,由于我编写程序的方式,我有两个函数相互调用,导致一个函数被隐式定义,有没有更简单的方法来做到这一点~?/我可以修复它吗?

/* print() */
static void print (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer)
{
fprintf (stderr , "print called");
if (print_pointer->printed == 0)
{
printf ("\nTitle: %s\n" , print_pointer->title);
printf ("Author: %s\n" , print_pointer->author);
printf ("Year: %i\n" , print_pointer->year);
print_pointer->printed = 1;
np++;
print_pointer = book_tree;
}
}

/* descend_right() */
void descend_right (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer)
{
fprintf (stderr , "descend_right called");
if (print_pointer->right->printed == 0)
{
print_pointer = print_pointer->right;
descend_left(title , author , year , printed , np , print_pointer);
}
if (print_pointer->right == NULL)
{
print(title , author , year , printed , np , print_pointer);
}
if (print_pointer->right->printed == 1)
{
descend_right(title , author , year , printed , np , print_pointer);
}
}

/* descend_left() */
void descend_left (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer)
{
fprintf (stderr , "descend_left called");
if (print_pointer->left->printed == 0)
{
print_pointer = print_pointer->left;
descend_left(title , author , year , printed , np , print_pointer);
}
if (print_pointer->left == NULL)
{
print(title , author , year , printed , np , print_pointer);
descend_right(title , author , year , printed , np , print_pointer);
}
if (print_pointer->left->printed == 1)
{
print(title , author , year , printed , np , print_pointer);
descend_right(title , author , year , printed , np , print_pointer);
}
}

/* menu_print_database(): */
static void menu_print_database (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int bn)
{
struct Book *print_pointer = book_tree;
int np = 0;
do
{
if (book_tree->left->printed == 1 && book_tree->right->printed == 0) print(title , author , year , printed , np , print_pointer);
if (print_pointer->printed == 0) descend_left (title , author , year , printed , np , print_pointer);
if (print_pointer->printed == 1) descend_right (title , author , year , printed , np , print_pointer);
}
while (np != bn);
}

最佳答案

您需要添加其中一个函数的前向声明,如下所示:

static void print (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer);
void descend_right (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer);
void descend_left (char title[MAX_TITLE_LENGTH+1] , char author[MAX_AUTHOR_LENGTH+1] , int year , int printed , int np , struct Book *print_pointer);

这是对编译器的 promise ,它会在稍后的某个时间在您的程序代码中定义这些函数。它告诉编译器函数的名称、它们的返回类型和它们的参数类型 - 产生函数调用所需知道的一切。

在第一个函数之前添加这三行来修复问题。

关于c - 尝试打印二叉树内容时的函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23592698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com