找不到这个简单的 C 代码有什么问题
Can't find what's wrong with this simple C code
简单的树插入遍历代码
#include<stdio.h>
#include<stdlib.h>
struct tree
{
struct tree *left;
struct tree *right;
int value;
};
typedef struct tree node;
void insertNode(node **root, int val)
{
node *temp = NULL;
if(!(*root))
{
temp = (node*) malloc(sizeof(node));
temp->value = val;
temp->left = NULL;
temp->right = NULL;
*root = temp;
}
if(val < (*root)->value)
insertNode(&(*root)->left, val);
if(val >= (*root)->value)
insertNode(&(*root)->right, val);
}
void preOrder(node *root)
{
if(root)
{ printf(" %d",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(node *root)
{
if(root)
{
inOrder(root->left);
printf(" %d",root->value);
inOrder(root->right);
}
}
void postOrder(node *root)
{
if(root)
{
postOrder(root->left);
postOrder(root->right);
printf(" %d",root->value);
}
}
void delTree(node *root)
{
if(root)
{
delTree(root->left);
delTree(root->right);
free(root);
}
}
int main()
{
int val;
char ch; ch = 'y';
node *root;
while(ch == 'y')
{
scanf("Enter the node value: %d", &val);
insertNode(&root, val);
scanf("Want to enter more: %c", &ch);
}
printf("\nInOrder traversal:\n");
inOrder(root);
printf("\nPreOrder traversal:\n");
preOrder(root);
printf("\nPostOrder traversal:\n");
postOrder(root);
delTree(root);
printf("Tree Deleted.");
return 0;
}
代码好像没有问题,也没有报错。虽然编译时显示警告;
ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute
这似乎是由于忽略了 scanf 的 return 值而启动的,来自 ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]? .
但即使在抑制警告和编译可执行文件时也没有接受任何输入。这是为什么。我在 CodeBlocks IDE.
上运行代码
ideone也是一样,http://ideone.com/jOnjEK .
如有任何帮助或建议,我们将不胜感激。
scanf("Enter the node value: %d", &val);
应该是
scanf("%d", &val);
和
scanf("Want to enter more: %c", &ch);
应该是
scanf(" %c", &ch);
假设您打算这样做
printf("Enter the node value:\n");
if(scanf("%d", &val) != 1)
{
printf("Integer not read \n");
break;
}
printf("Want to enter more:\n");
if(scanf(" %c", &ch) != 1)
{
printf("character not read\n");
break;
}
记下 scanf()
中 %c
之前的 space
简单的树插入遍历代码
#include<stdio.h>
#include<stdlib.h>
struct tree
{
struct tree *left;
struct tree *right;
int value;
};
typedef struct tree node;
void insertNode(node **root, int val)
{
node *temp = NULL;
if(!(*root))
{
temp = (node*) malloc(sizeof(node));
temp->value = val;
temp->left = NULL;
temp->right = NULL;
*root = temp;
}
if(val < (*root)->value)
insertNode(&(*root)->left, val);
if(val >= (*root)->value)
insertNode(&(*root)->right, val);
}
void preOrder(node *root)
{
if(root)
{ printf(" %d",root->value);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(node *root)
{
if(root)
{
inOrder(root->left);
printf(" %d",root->value);
inOrder(root->right);
}
}
void postOrder(node *root)
{
if(root)
{
postOrder(root->left);
postOrder(root->right);
printf(" %d",root->value);
}
}
void delTree(node *root)
{
if(root)
{
delTree(root->left);
delTree(root->right);
free(root);
}
}
int main()
{
int val;
char ch; ch = 'y';
node *root;
while(ch == 'y')
{
scanf("Enter the node value: %d", &val);
insertNode(&root, val);
scanf("Want to enter more: %c", &ch);
}
printf("\nInOrder traversal:\n");
inOrder(root);
printf("\nPreOrder traversal:\n");
preOrder(root);
printf("\nPostOrder traversal:\n");
postOrder(root);
delTree(root);
printf("Tree Deleted.");
return 0;
}
代码好像没有问题,也没有报错。虽然编译时显示警告;
ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute
这似乎是由于忽略了 scanf 的 return 值而启动的,来自 ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]? .
但即使在抑制警告和编译可执行文件时也没有接受任何输入。这是为什么。我在 CodeBlocks IDE.
上运行代码ideone也是一样,http://ideone.com/jOnjEK .
如有任何帮助或建议,我们将不胜感激。
scanf("Enter the node value: %d", &val);
应该是
scanf("%d", &val);
和
scanf("Want to enter more: %c", &ch);
应该是
scanf(" %c", &ch);
假设您打算这样做
printf("Enter the node value:\n");
if(scanf("%d", &val) != 1)
{
printf("Integer not read \n");
break;
}
printf("Want to enter more:\n");
if(scanf(" %c", &ch) != 1)
{
printf("character not read\n");
break;
}
记下 scanf()
%c
之前的 space