请大家看看这个程序是怎么了,怎么不出结果呢? 真是郁闷,请教! #include <stdio.h> #include<malloc.h> #define MaxSize 100 typedef char ElemType; typedef struct node { ElemType data; struct node *left,*right; } BTree;
void creatree(BTree **BT,char *str) { BTree *stack[MaxSize],*p; int top=-1,k,j=0; char ch; *BT=NULL; ch=str[j]; while (ch!='\0') { switch(ch) { case '(':top++;stack[top]=p;k=1; break; case ')':top--; break; case ',':k=2; break; default: p=(BTree *)malloc(sizeof(BTree)); p->data=ch;p->left=p->right=NULL; if (*BT==NULL) *BT=p; else { switch(k) { case 1:stack[top]->left=p; break; case 2:stack[top]->right=p; } } } j++; ch=str[j]; } } void inorder(BTree *BT) { if (BT!=NULL) { inorder(BT->left); printf("%c",BT->data); inorder(BT->right); } }
main() { char st[20]; BTree *B; printf("input :"); scanf("%s",st); gets(st); puts(st); creatree(&B,st);
printf("\nresult is:"); inorder(B); getch(); }