先序遍历建树及其输出(C和C++的不同)

破碎山河 2014-05-26 08:16:44

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include <iostream>
using namespace std;

struct tree;
typedef struct tree *Tree;

struct tree
{
char data;
Tree left;
Tree Right;
};

void CreatTree(Tree T)
{
char ch;
cin>>ch;
if(ch=='#')
return;
else
{
T=(Tree)malloc(sizeof(struct tree));
T->data=ch;
T->left=T->Right=NULL;
CreatTree(T->left);
CreatTree(T->Right);
}
}

void Print(Tree T)
{
while(T!=NULL)
{
printf("%c",T->data);
Print(T->left);
Print(T->Right);
}
}

int main()
{
Tree T=NULL;
CreatTree(T);
Print(T);
return 0;
}


这是我自己写的C版本、


#include<iostream>
using namespace std;
struct tree
{
char a;
tree *lchild,*rchild;
};
void set(tree *&b)
{
char ch;
cin>>ch;
if(ch=='#') return;
else
{
b=new tree;
b->a=ch;
b->lchild=b->rchild=NULL;
set(b->lchild);
set(b->rchild);
}
}
void print(tree *b)
{
if(b!=NULL)
{
cout<<b->a;
print(b->lchild);
print(b->rchild);
}
}
int main()
{
tree *b=NULL;
set(b);
print(b);
return 0;
}


这是朋友写的C++版本,

可以看出来在这两段函数上不同,我现在不知道怎么样修改,才能让指针传递符合要求?(即用C实现,而非用C++)


void Print(Tree T)
{
while(T!=NULL)
{
printf("%c",T->data);
Print(T->left);
Print(T->Right);
}
}
void print(tree *b)
{
if(b!=NULL)
{
cout<<b->a;
print(b->lchild);
print(b->rchild);
}
}


另外,前面有段时间去住院了,所以没有及时结贴,不好意思!
...全文
336 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
破碎山河 2014-05-29
  • 打赏
  • 举报
回复
.。。。。楼上的博客灰常好!!!了解!
Morrisss_ 2014-05-28
  • 打赏
  • 举报
回复
引用 3 楼 u012514377 的回复:
你们木有注意看啊→_→,我写的typedef把结构体显化为指针,但是问题在print那个函数里,C++版本的print函数传递的指针是*tree,而非*&tree,我用了typedef后,Tree和*&tree相同,但和*tree不同啊!我的意思是,在用了这个typedef的情况下,怎么表示那个C++版本里的tree *b?
谁告诉你的Tree和*&tree相同。。。一个是指针变量,一个是指针的引用。。。你这C和C++版本写的就是差不多的

struct tree;
typedef struct tree *Tree;

void print(tree *b)
{
    if(b!=NULL)
    {
        cout<<b->a;
        print(b->lchild);
        print(b->rchild);
    }
}
lanlvy 2014-05-28
  • 打赏
  • 举报
回复
http://blog.csdn.net/u012514171/article/details/27202885
破碎山河 2014-05-28
  • 打赏
  • 举报
回复
@zhao4zhogn1,我说的是print那个函数,不是printf→_→, 另外回楼上,吐血了我要, 我的意思是用了typedef后,这个函数不行 在C里应该怎样修改print这个函数(函数!!!)的参数,让它符合C版本typedef里的设置?(即只能使用Tree T,或者struct tree

void print(Tree b)//注意看参数!!!!!!
{
    if(b!=NULL)
    {
        cout<<b->a;
        print(b->lchild);
        print(b->rchild);
    }
}
破碎山河 2014-05-27
  • 打赏
  • 举报
回复
你们木有注意看啊→_→,我写的typedef把结构体显化为指针,但是问题在print那个函数里,C++版本的print函数传递的指针是*tree,而非*&tree,我用了typedef后,Tree和*&tree相同,但和*tree不同啊!我的意思是,在用了这个typedef的情况下,怎么表示那个C++版本里的tree *b?
l5250926 2014-05-27
  • 打赏
  • 举报
回复
void print(tree *b) { if(*b!=NULL) { printf("%d",(*b)->a); print(&(*b)->lchild); print(&(*b)->rchild); } }
赵4老师 2014-05-27
  • 打赏
  • 举报
回复
乍看起来c++的cin、cout在输入、输出上比c的scanf、printf简单,不用格式控制符! 但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。 摒弃cin、cout! 使用scanf、printf。
buyong 2014-05-27
  • 打赏
  • 举报
回复
把cout换成printf
Morrisss_ 2014-05-26
  • 打赏
  • 举报
回复
写的是一样的啊。你没看到上面的typedef嘛?你朋友用的Tree其实就是个tree*类型。

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧