创建简单的二叉树
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
struct Snode *lchild;
struct Snode *rchild;
int data;
}*Node,Snode;
#include <stdlib.h>
typedef struct Node{
struct Snode *lchild;
struct Snode *rchild;
int data;
}*Node,Snode;
void create(Node *l){
int c;
scanf("%d",&c);//若是使用字符类型则会跳转,因为将enter也吸收进缓冲区
if(c==0){
*l = NULL;//子叶
}
else{
*l = (Snode *)malloc(sizeof(Snode));
(*l)->data = c;
create(&((*l)->lchild));
create(&((*l)->rchild));
}
}
void print(Node *l){
int c;
if(*l){
c = (*l)->data;
printf("%d",c);
print(&(*l)->lchild);
print(&(*l)->rchild);
}
}
int c;
scanf("%d",&c);//若是使用字符类型则会跳转,因为将enter也吸收进缓冲区
if(c==0){
*l = NULL;//子叶
}
else{
*l = (Snode *)malloc(sizeof(Snode));
(*l)->data = c;
create(&((*l)->lchild));
create(&((*l)->rchild));
}
}
void print(Node *l){
int c;
if(*l){
c = (*l)->data;
printf("%d",c);
print(&(*l)->lchild);
print(&(*l)->rchild);
}
}
void main(){
Node l = NULL;
create(&l);
print(&l);
}
Node l = NULL;
create(&l);
print(&l);
}
静,静,静