树的非递归算法
/* Note:Your choice is C IDE */#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
char ch;
struct bitnode *lchild,*rchild;
}bitnode,*bittree;
typedef struct stacknode
{
bitnode *data;
struct stacknode *next;
}linkstack;
void initstack(linkstack *s)
{ s->next=NULL; }
void push(linkstack *s,bittree x)
{
linkstack *p;
p=(linkstack *)malloc(sizeof(linkstack));
p->data=x;
p->next=s->next;
s->next=p;
}
bittree pop(linkstack *s)
{
bittree x;
linkstack *p;
p=s->next;
if(s->next==0)return 0;
x=p->data;
s->next=p->next;
free(p);
return x;
}
bittree cre_tree()
{
bittree root;
char ch;
ch=getchar();
if(ch=='#')root=NULL;
else
{
root=(bittree)malloc(sizeof(bitnode));
root->ch=ch;
root->lchild=cre_tree();
root->rchild=cre_tree();
}
return root;
}
void preorder(bittree root)
{
linkstack s;
bittree p;
push(&s,root);
while(s.next)
{
p=pop(&s);
while(p)
{
printf("%c",p->ch);
if(p->rchild)push(&s,p->rchild);
p=p->lchild;
}
}
}
main()
{
bittree root;
root=cre_tree();
preorder(root);
}
帮我再调调一下,为什么运行后还提示错误。