首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

二叉树代码,输出时出现有关问题,求高手

2013-03-06 
二叉树代码,输出时出现问题,求高手!#include stdio.h#include stdlib.h#include ctype.hstruct Node

二叉树代码,输出时出现问题,求高手!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct Node
{
int item  ;
int count ;
struct Node *left  ;
struct Node *right  ;
};
struct Node *creatnode(long value)
{

struct Node *pnode = (struct Node *)malloc(sizeof(struct Node));
pnode->item = value ;
pnode->count = 1 ;
pnode->left = pnode->right = NULL ;
return pnode ;


}
struct Node *addnode(long value, struct Node *pnode)
{

if (pnode == NULL)
return creatnode(value) ;
if (value = pnode->item)
{
++ pnode->count ;
return pnode ;
}
if (value < pnode->item)
{
if (pnode->left = NULL)
{
pnode->left = creatnode(value);
return pnode->left;
}
else
return addnode(value, pnode->left); /* 传说中的递归调用! */
}
else
{
if (pnode->right = NULL)
{
pnode->right = creatnode(value);
return pnode->right ;
}
else
return addnode(value, pnode->right);
}
}
void listnodes(struct Node *pnode)
{
int i = 0 ;
if(pnode->left != NULL)
listnodes(pnode->left);
for(i = 0 ; i < pnode->count; i ++)
printf("%d\n", pnode->item) ;
if(pnode->right != NULL)
listnodes(pnode->right);
}
void freenodes(struct Node *pnode)
{
if (pnode == NULL)
return 0;
if(pnode->left != NULL)
freenodes(pnode->left);
if(pnode->right != NULL)
freenodes(pnode->right);
free(pnode) ;
}
int main (void)
{

long value = 0 ;
struct Node *proot = NULL;
char test = 'n' ;
do
{
printf("Enter a number:\n");
scanf("%ld", &value);
getchar();
if(proot = NULL)
proot = creatnode(value) ;
else
addnode(value, proot);
printf("Do you want to enter another number?(Y or N)\n");
scanf("%c", &test);
}while (tolower(test) == 'y');
printf("The numbers are:\n");
listnodes (proot) ;
freenodes (proot) ;
return 0 ;
}


c
[解决办法]
先把if里面的=换成==再说吧

热点排行